semvec/pypi-packages huggingface dataset:
zenetka 1.3.0
Structured formatter for Python's logging
License: BSD

zenetka
=======

**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