Skip to main content

JSON formatter for logging

pypi version Build status Coverage Code style: black Number of tests Number of downloads

This module provides a JSON formatter for the python logging module that will format to JSON formatted string.

Using this formatter allows to have the proper format for logging to Splunk or ElasticSearch, but it can also be used for logging to stdout as a string is issued.

Features

Adding additional fields and values

You can add fields to every message that is being logged. To do so, specify the fields parameter to the logging_json.JSONFormatter instance.

It must be a dictionary where keys are the keys to be appended to the resulting JSON dictionary (if not already present) and the values can be one of the following:

  • An attribute of the logging record (non-exhaustive list can be found on the python logging documentation).
  • If not found on the record, the value will be linked to the key.

Logging exceptions, a specific case

If an exception is logged, the exception key will be appended to the resulting JSON dictionary.

This dictionary will contain 3 keys:

  • type: The name of the exception class (useful when the message is blank).
  • message: The str representation of the exception (usually the provided error message).
  • stack: The stack trace, formatted as a string.

You can rename the exception field key by setting the exception_field_name parameter with a new name for the key. It is also possible to disable this behaviour by setting the exception_field_name parameter to None or an empty string

Logging with a dictionary

This formatter allows you to log dictionary as in the following:

import logging

logging.info({"key": "value", "other key": "other value"})

The resulting JSON dictionary will be the one you provided (with the additional fields).

Logging with anything else (such as a string)

Anything not logged using a dictionary will be handled by the standard formatter, and it can result in one of the 2 output:

  • A JSON dictionary, if additional fields are set or if extra parameter is used while logging, with the message available in the message key of the resulting JSON dictionary. Default message key name can be changed by message_field_name parameter of the logging_json.JSONFormatter instance.
  • The formatted record, if no additional fields are set.

This handles the usual string logging as in the following:

import logging

logging.info("This is my message")

Changing asctime representation

You can override the default representation of asctime (2003-07-08 16:49:45,896) based on two different scenarii:

Without milliseconds

Set datefmt parameter.

Setting datefmt to %Y-%m-%dT%H:%M:%S would result in 2003-07-08T16:49:45.

With milliseconds

Set default_time_format to something else than %Y-%m-%d %H:%M:%S to change the representation part without milliseconds. Set default_msec_format to something else than %s,%03d to change the representation milliseconds. Note that %s in default_msec_format is going to be replaced by the representation without milliseconds.

Setting default_time_format to %Y-%m-%dT%H:%M:%S and default_msec_format to %s.%03d would result in 2003-07-08T16:49:45.896.

Configuration

You can create a formatter instance yourself as in the following, or you can use a logging configuration.

import logging_json

formatter = logging_json.JSONFormatter(fields={
    "level_name": "levelname",
    "thread_name": "threadName",
    "process_name": "processName"
})

Using logging.config.dictConfig

You can configure your logging as advertise by python, by using the logging.config.dictConfig function.

dict configuration

import logging.config

logging.config.dictConfig({
    "version": 1,
    "formatters": {
        "json": {
            '()': 'logging_json.JSONFormatter',
            'fields':{
                "level_name": "levelname",
                "thread_name": "threadName",
                "process_name": "processName"
            }
        }
    },
    "handlers": {
        "standard_output": {
            'class': 'logging.StreamHandler',
            'formatter': 'json',
            'stream': 'ext://sys.stdout'
        },
    },
    "loggers": {
        "my_app": {"level": "DEBUG"}
    },
    "root": {
        "level": "INFO",
        "handlers": ["standard_output"]
    }
})

YAML logging configuration

You can use YAML to store your logging configuration, as in the following sample:

import logging.config
import yaml

with open("path/to/logging_configuration.yaml", "r") as config_file:
    logging.config.dictConfig(yaml.load(config_file))

Where logging_configuration.yaml can be a file containing the following sample:

version: 1
formatters:
  json:
    '()': logging_json.JSONFormatter
    fields:
      level_name: levelname
      thread_name: threadName
      process_name: processName
handlers:
  standard_output:
    class: logging.StreamHandler
    formatter: json
    stream: ext://sys.stdout
loggers:
  my_app:
    level: DEBUG
root:
  level: INFO
  handlers: [standard_output]

How to install

  1. python 3.7+ must be installed
  2. Use pip to install module:
python -m pip install logging_json

Metadata

Release files for logging-json 0.6.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 logging-json 0.6.0
File Size Uploaded Provenance
logging_json-0.6.0.tar.gz 12.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for logging-json 0.6.0
File Interpreter ABI Platform
logging_json-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.5 kB

Release files / logging_json-0.6.0.tar.gz

Download URL logging_json-0.6.0.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
48c15b0ed97d590642a13b2c5f44d6fd4eda5d9af970baacf5107e92cfcd35b0
BLAKE2b-256 checksum
How to use checksums
f9c7024c4462b3d9f89d1772545d4514a1c02e941734951ad357461b255e38a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / logging_json-0.6.0-py3-none-any.whl

Download URL logging_json-0.6.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
962c377f69fdbcad2a2267626214bf79e85d112de3e38e7643994c1aeb640c85
BLAKE2b-256 checksum
How to use checksums
85b92edeb00896fbf67918fa5caa31d3e9f98c8abc9984c7d75e164611e70232
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Security for logging-json 0.6.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.

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