Python JSON Logger

raw JSON →
4.0.0 verified Tue May 12 auth: no python install: verified quickstart: verified

A JSON log formatter for the Python logging package that supports various JSON encoders. Current version is 4.0.0, with regular releases typically made every few months.

pip install python-json-logger
error ModuleNotFoundError: No module named 'json_log_formatter'
cause The `python-json-logger` package, which provides the `json_log_formatter` module, is not installed in the current Python environment.
fix
pip install python-json-logger
error ModuleNotFoundError: No module named 'python_json_logger'
cause Although the package is named `python-json-logger`, the main module containing the `JsonFormatter` class is `json_log_formatter`.
fix
from json_log_formatter import JsonFormatter
error ImportError: cannot import name 'JsonLogger' from 'json_log_formatter'
cause The class name `JsonLogger` is incorrect; the correct class for JSON formatting in this library is `JsonFormatter`.
fix
from json_log_formatter import JsonFormatter
error TypeError: Object of type <ClassName> is not JSON serializable
cause A log record contains an object (e.g., datetime, custom class instance) that the default JSON encoder cannot serialize, and no custom `json_default` function or `json_encoder` class was provided to `JsonFormatter`.
fix
Configure JsonFormatter with a json_default function to convert non-serializable objects into a serializable format:
import datetime
from json_log_formatter import JsonFormatter

def custom_serializer(o):
    if isinstance(o, datetime.datetime):
        return o.isoformat()
    raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")

formatter = JsonFormatter(json_default=custom_serializer)
error TypeError: JsonFormatter.__init__() got an unexpected keyword argument 'some_arg'
cause An unrecognized or misspelled keyword argument was passed to the `JsonFormatter` constructor.
fix
Refer to the library's documentation for the correct list of supported arguments for JsonFormatter, such as json_default, json_encoder, json_message_key, json_extra, etc.
breaking The use of some older imports has changed, e.g., 'from pythonjsonlogger.jsonlogger import jsonlogger' is incorrect.
fix Migrate to 'from pythonjsonlogger.jsonlogger import JsonFormatter'.
gotcha Ensure that required fields are correctly specified in comma-separated format to avoid formatting issues.
fix Use the new formatter syntax that supports comma-separated lists.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.07s 17.9M
3.10 slim (glibc) - - 0.04s 18M
3.11 alpine (musl) - - 0.11s 19.7M
3.11 slim (glibc) - - 0.10s 20M
3.12 alpine (musl) - - 0.09s 11.6M
3.12 slim (glibc) - - 0.09s 12M
3.13 alpine (musl) - - 0.08s 11.2M
3.13 slim (glibc) - - 0.11s 12M
3.9 alpine (musl) - - 0.07s 17.7M
3.9 slim (glibc) - - 0.06s 18M

Basic usage of JsonFormatter to log messages in JSON format.

import logging
from pythonjsonlogger.jsonlogger import JsonFormatter

logger = logging.getLogger('my_logger')
handler = logging.StreamHandler()
formatter = JsonFormatter()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info('This is a log message with JSON formatting')