Skip to main content

adapted_logger is a lib based on Python logging module, permetting injection of new data on logsand redirection to different target

Project description

A helper log library based on default logging module permetting a custom format of logs to be redirected to Logstash - Elasticsearch - Kibana.


Custom usage:

HOW TO INSTALL

  • Install adapted_logger using easy_setup or pip:

    pip install adapted_logger

HOW TO USE:

from logger.adapted_logger import AdaptedLogger
logger = AdaptedLogger("project_name", "127.0.0.1") # specify project_name and ip address of current server
log = logger.get_logger()
log.info("This is an info message")
log.debug("This is a debug message")
log.warn("This is a warning message")
log.error("This is an error message")

RESULTS:

2015-10-27 17:06:50,176 project_name INFO 127.0.0.1 This is an info message
2015-10-27 17:06:55,552 project_name DEBUG 127.0.0.1 This is a debug message
2015-10-27 17:07:00,863 project_name WARNING 127.0.0.1 This is a warning message
2015-10-27 17:07:05,360 project_name ERROR 127.0.0.1 This is an error message

Redirect logs to console:

Instantiate AdaptedLogger object:

adapted_log = AdaptedLogger("retail_crm_server", "127.0.0.1")

Redirect logs to console (Default behavior):

adapted_log.redirect_to_console()

Get logger object:

logger = adapted_log.get_logger()
logger.debug("Testing Debug Message")
logger.info("Testing Info Message")
logger.warn("Testing Warn Message")
logger.error("Testing Error Message")

Redirect logs to file:

Instantiate AdaptedLogger object:

adapted_log = AdaptedLogger("retail_crm_server", "127.0.0.1")

Redirect logs to file:

adapted_log.redirect_to_file("/path/logfile.log")

Get logger object:

logger = adapted_log.get_logger()
logger.debug("Testing Debug Message")
logger.info("Testing Info Message")
logger.warn("Testing Warn Message")
logger.error("Testing Error Message")

Custom adapter

This adds the possibility to configure your logger within a config file like config.yml, inside /config folder you should specify your configuration in YAML format.

formatters:

simpleFormater:
    format: '%(asctime)s    %(name)s    %(levelname)s    %(message)s'
    datefmt: '%Y/%m/%d %H:%M:%S's

Here we specify time, package name, level name, and the message, right now there is no injection of the ip address

handlers

console:
    class: logging.StreamHandler
    formatter: simpleFormater
    level: DEBUG
    stream: ext://sys.stdout
file:
    class : logging.FileHandler
    formatter: simpleFormater
    level: WARNING
    filename: filename_or_path.log

Here we specify handlers, in this exemple we use 2 handlers : console and file.

loggers:

clogger:
    level: DEBUG
    handlers: [console]
flogger:
    level: WARNING
    handlers: [file]

And finally we create 2 loggers for console and file handlers.

To inject ip address into context, we have created CustomAdapter class that create an adapter and inject ip in the process.

Usage

logging_config = yaml.load(open('config/config.yml', 'r'))
dictConfig(logging_config)

logger_1 = logging.getLogger("project_name.application_name1")
logger_1 = CustomAdapter(logger_1, {'ip': ip})

logger_2 = logging.getLogger("project_name.application_name2")
logger_2 = CustomAdapter(logger_2, {'ip': ip})

Right now, we can call

logger_1.warning('This is a warning Message')
logger_2.error('This is an error message)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adapted_logger-1.0.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

adapted_logger-1.0.4-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file adapted_logger-1.0.4.tar.gz.

File metadata

File hashes

Hashes for adapted_logger-1.0.4.tar.gz
Algorithm Hash digest
SHA256 8dbdf4baa2942525d6bef778bdf6ea07d82d8e77c9f109ab67df71badf03e640
MD5 7d888485cc628ca689b27d9e42384641
BLAKE2b-256 c5bb942ae819a4e46eb2caa7d027a5b8b46db090eddd5962ea2e59cd7e24e3f4

See more details on using hashes here.

File details

Details for the file adapted_logger-1.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for adapted_logger-1.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7d8f8744e44bd870b81f17167e9717f453fff90607d031e190d87c6be1115452
MD5 92f4bd373bd1c0df28a95aaf8e7a693d
BLAKE2b-256 9461dca4011cb303b8f616679d703a57b089bc7c8cad47171e8ec06b939d99c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page