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 JsonFormatter, init_json_logger

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.1.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_json_logging-1.1.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple-json-logging-1.1.2.tar.gz
  • Upload date:
  • Size: 3.8 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.1.2.tar.gz
Algorithm Hash digest
SHA256 5bb9e2841d5429c1d56386fe2312426e687e810190c768e1c3ffba4acefa4ae8
MD5 a78f082d860e4f10feee684ff5309c68
BLAKE2b-256 2c0c83cbc4eae465856d1538f72acd49aa4d6d85c71b10f128926ccfea051734

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_json_logging-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6d9e3bc573f489be6017ef5a2bbd5b7a2754f4ab554cc7ef8f91689011de4a3f
MD5 76b3f955b17ea131fd124750d2988c06
BLAKE2b-256 f4bdd93d0d089bad92963723395d0281687bc1887c5a09f82da5a9abb06d026e

See more details on using hashes here.

Supported by

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