Skip to main content

Library for structured logging via JSON document

Project description

Library for structured logging in Python

Examples

Simple script for starting:

from simple_json_logging import init_json_logger

logger = init_json_logger('my_json_logger')


def main():
    logger.warning('Test log message with arg1 %s and arg2 %s', 'abc', 100, foo='yyy', bar=100500)
    try:
        raise RuntimeError('just for test')
    except Exception:
        logger.exception('exception', foo='yyy')


if __name__ == '__main__':
    main()

This script will output:

{"name": "my_json_logger", "levelname": "WARNING", "levelno": 30, "pathname": "example.py", "filename": "example.py", "module": "example", "stack_info": null, "lineno": 7, "funcName": "main", "created": 1564591269.778053, "msecs": 778.0530452728271, "relativeCreated": 4.003047943115234, "thread": 30272, "threadName": "MainThread", "processName": "MainProcess", "process": 19180, "data": {"foo": "yyy", "bar": 100500}, "message": "Test log message with arg1 abc and arg2 100"}
{"name": "my_json_logger", "levelname": "ERROR", "levelno": 40, "pathname": "example.py", "filename": "example.py", "module": "example", "stack_info": null, "lineno": 11, "funcName": "main", "created": 1564591269.778053, "msecs": 778.0530452728271, "relativeCreated": 4.003047943115234, "thread": 30272, "threadName": "MainThread", "processName": "MainProcess", "process": 19180, "data": {"foo": "yyy"}, "message": "exception", "exceptionClass": "RuntimeError", "exceptionMessage": "just for test"}

Another one with a custom formatter:

from simple_json_logging import init_json_logger, JsonFormatter

formatter = JsonFormatter(json_dumps_args={'sort_keys': True, 'indent': 2})
logger = init_json_logger('my_json_logger', formatter=formatter)


def main():
    logger.warning('Test log message with arg1 %s and arg2 %s', 'abc', 100, foo='yyy', bar=100500)
    try:
        raise RuntimeError('just for test')
    except Exception:
        logger.exception('exception', foo='yyy')


if __name__ == '__main__':
    main()

This script will output:

{
  "created": 1564591638.1781173,
  "data": {
    "bar": 100500,
    "foo": "yyy"
  },
  "exc_text": null,
  "filename": "example.py",
  "funcName": "main",
  "levelname": "WARNING",
  "levelno": 30,
  "lineno": 8,
  "message": "Test log message with arg1 abc and arg2 100",
  "messageFormatted": "Test log message with arg1 abc and arg2 100",
  "module": "example",
  "msecs": 178.1172752380371,
  "name": "my_json_logger",
  "pathname": "example.py",
  "process": 26544,
  "processName": "MainProcess",
  "relativeCreated": 2.9993057250976562,
  "stack_info": null,
  "thread": 28208,
  "threadName": "MainThread"
}
{
  "created": 1564591638.1781173,
  "data": {
    "foo": "yyy"
  },
  "exc_text": "Traceback (most recent call last):\n  File \"example.py\", line 10, in main\n    raise RuntimeError('just for test')\nRuntimeError: just for test",
  "exceptionClass": "RuntimeError",
  "exceptionMessage": "just for test",
  "filename": "example.py",
  "funcName": "main",
  "levelname": "ERROR",
  "levelno": 40,
  "lineno": 12,
  "message": "exception",
  "messageFormatted": "exception",
  "module": "example",
  "msecs": 178.1172752380371,
  "name": "my_json_logger",
  "pathname": "example.py",
  "process": 26544,
  "processName": "MainProcess",
  "relativeCreated": 2.9993057250976562,
  "stack_info": null,
  "thread": 28208,
  "threadName": "MainThread"
}

Known issues

Didn't tested on versions before Python 3.7, please give me feedback about previous versions.

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

simple-json-logging-1.0.5.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

simple_json_logging-1.0.5-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file simple-json-logging-1.0.5.tar.gz.

File metadata

  • Download URL: simple-json-logging-1.0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for simple-json-logging-1.0.5.tar.gz
Algorithm Hash digest
SHA256 929c63284e4e33cec271bf19223c9cf89c9383646194d74685ab1df083f70342
MD5 53f26df9ddc821833aac68a507f215be
BLAKE2b-256 4a655388fa5820e3af2f7fed82fcb1fc75500a7d5cd88764d662668937040193

See more details on using hashes here.

File details

Details for the file simple_json_logging-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: simple_json_logging-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for simple_json_logging-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 15c83b2a5a9f180051d79aebf82185234f47e44547f0d592a230195922368902
MD5 5ae7ffd1b54714f2d79e2b589b1decb7
BLAKE2b-256 47a14055149fdd5c1f2d11c92dee3e9d982346bc5be83dfdaed5605b04cd37a2

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