jsonformatter is a Python library that provides a logging formatter to output log records as JSON objects. It allows for easy customization of LogRecord attributes, enabling users to include or replace fields, for instance, to integrate with log aggregation systems like Logstash. It supports Python 2.7 and Python 3. The latest version, 0.3.4, was released in November 2024, with an irregular but ongoing release cadence.
pip install jsonformatterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates two ways to use `jsonformatter`: first, using `basicConfig` for a quick setup, and second, by creating a `JsonFormatter` instance with a custom format string and attaching it to a `StreamHandler`. It also shows how to include extra fields in log messages and handle exceptions.
Verify the PyPI package name (`jsonformatter` vs `python-json-logger`) and the GitHub repository (`MyColorfulDays/jsonformatter` vs `nhairs/python-json-logger`) to ensure you are installing and using the intended library.
Pass a `default` callable or a custom `json.JSONEncoder` subclass via the `cls` parameter to the `JsonFormatter` constructor to define how non-serializable objects should be converted. For example, `JsonFormatter(format_string, default=str)` will convert such objects to their string representation.
For new development, use Python 3 exclusively. For existing projects, consider migrating to Python 3. `jsonformatter` is also compatible with Python 3, so ensure your environment uses a supported Python 3 version.
Ensure the library is installed using pip: `pip install jsonformatter`.
Use the correct class name for the logging formatter: `from jsonformatter import JsonFormatter`.
You need to provide a custom JSON encoder to `JsonFormatter` to handle non-serializable objects. This can be done by passing a `json_default` function or a `cls` (JSONEncoder subclass) parameter to `JsonFormatter` that defines how to serialize these types.
Review your `format` string or dictionary configuration to ensure all specified `LogRecord` attributes (like `%(levelname)s`, `%(message)s`, `%(name)s`, or custom attributes) are valid and present in the log record. Ensure any custom attributes are properly added to the `LogRecord.extra` dictionary if you intend to include them.
No dependency data recorded yet.