Skip to content

Python JSON Logger🔗

License Build Status

Introduction🔗

Python JSON Logger enables you produce JSON logs when using Python's logging package.

JSON logs are machine readable allowing for much easier parsing and ingestion into log aggregation tools.

This library assumes that you are famliar with the logging standard library package; if you are not you should start by reading the official Logging HOWTO.

Warning

This repository is a maintained fork of madzak/python-json-logger pending a PEP 541 request for the PyPI package. The future direction of the project is being discussed here.

Features🔗

  • Standard Library Compatible: Implement JSON logging without modifying your existing log setup.
  • Supports Multiple JSON Encoders: In addition to the standard libary's json module, also supports the orjson, msgspec JSON encoders.
  • Fully Customizable Output Fields: Control required, excluded, and static fields including automatically picking up custom attributes on LogRecord objects. Fields can be renamed before they are output.
  • Encode Any Type: Encoders are customized to ensure that something sane is logged for any input including those that aren't supported by default. For example formatting UUID objects into their string representation and bytes objects into a base 64 encoded string.

Quick Start🔗

Follow our Quickstart Guide.

TLDR
import logging
from pythonjsonlogger.json import JsonFormatter

logger = logging.getLogger()
logger.setLevel(logging.INFO)

handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())

logger.addHandler(handler)

logger.info("Logging using pythonjsonlogger!", extra={"more_data": True})

# {"message": "Logging using pythonjsonlogger!", "more_data": true}

Bugs, Feature Requests etc🔗

Please submit an issue on github.

In the case of bug reports, please help us help you by following best practices 1 2.

In the case of feature requests, please provide background to the problem you are trying to solve so that we can a solution that makes the most sense for the library as well as your use case.

License🔗

This project is licensed under the BSD 2 Clause License - see LICENSE

Authors and Maintainers🔗

This project was originally authored by Zakaria Zajac and our wonderful contributors

It is currently maintained by: