Skip to main content

Structured formatter for Python's logging

Project description

zenetka provides simple Python’s logging formater which formats logging.LogRecord to JSON object.

Instalation

pip install zenetka

Usage

Configure your logging. For example, if you use logging.config.dictConfig() format:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'json': {
            '()': 'zenetka.JsonFormatter',
            'datefmt': '%Y-%m-%d %H:%M:%S',
            'ignored_attrs': ['processName', 'thread', 'threadName'],
            'pretty': False,
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'NOTSET',
            'formatter': 'json',
        },
    },
    'loggers': {
        '': {
            'level': 'INFO',
            'handlers': ('console',),
        },
    },
}

Example of the logged message:

{"name":"doporucovani_storage.couchbasestorage","msg":"Connecting to Couchbase \"%s\"","args":["http:\/\/localhost:8091\/bucket"],"levelname":"INFO","levelno":20,"module":"couchbasestorage","exc_text":"","funcName":"connect","created":1541512363.4698207378,"msecs":469.8207378387,"relativeCreated":2155.5094718933,"process":19816,"message":"Connecting to Couchbase \"http:\/\/localhost:8091\/bucket\"","asctime":"2018-11-06 14:52:43","stack_text":""}

If pretty is True, attribute names will be sorted and indented. ignored_attrs is a list of the attributes which will be excluded (for all attribute names see https://docs.python.org/3/library/logging.html#logrecord-attributes). exc_info and stack_info are always excluded, they are converted to exc_text and stack_text.

Do you want to log additional data? Instead of args use one argument as dict or use extra argument or both:

logger.info(
    'Connecting to Couchbase "%(connection_string)s"',
    {'connection_string': conn_str, 'attempts': attempts},
    extra={'msg_type': 'DB_CONNECT'})

extra are converted to JSON object’s atributes. dict is used for string formatting, it can contain additional attributes. Will produce message:

{"msg":"Connecting to Couchbase \"%(connection_string)s\"","args":{"connection_string":"http:\/\/localhost:8091\/bucket","attempts":2},"msg_type":"DB_CONNECT","message":"Connecting to Couchbase \"http:\/\/localhost:8091\/bucket\"","asctime":"2018-11-06 14:52:43","stack_text":"", ...}

So use Python’s logging and enjoy structured logs. However text logs are still availble, because there is not magic, is’s still Python’s logging. It depends on configuration, so use logging.Formatter for classic text logs:

2018-11-06 14:52:43 INFO Connecting to Couchbase "http://localhost:8091/bucket"

License

3-clause BSD

Project details


Download files

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

Files for zenetka, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size zenetka-1.0.0.tar.gz (4.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page