Skip to main content

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.

Metadata

Release files for simple-json-logging 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-json-logging 2.0.0
File Size Uploaded Provenance Actions
simple-json-logging-2.0.0.tar.gz 3.9 kB Without provenance Details for simple-json-logging-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-json-logging 2.0.0
File Interpreter ABI Platform Actions
simple_json_logging-2.0.0-py3-none-any.whl Python 3 none any Details for simple_json_logging-2.0.0-py3-none-any.whl

Total release size: 8.8 kB

Release files / simple-json-logging-2.0.0.tar.gz

Download URL simple-json-logging-2.0.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8712e96b03ee962c22108b4de800fbd4defc36b00ad826511927dbe76de5a015
BLAKE2b-256 checksum
How to use checksums
86ca2c8f352ee53a64611a7eb1b6ceb388af0588d87251ab25d14a1c7a9abe29
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / simple_json_logging-2.0.0-py3-none-any.whl

Download URL simple_json_logging-2.0.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08da496112a1972a0c0412a3bdad39a0a063ea97afeafef4f5738022eb771db0
BLAKE2b-256 checksum
How to use checksums
20bc002c958a4752fbb8483341bbf0872cf8933b7dc27133a338abfcd3427b1e
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for simple-json-logging 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page