Python JSON Logger
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.
Warnings
- breaking The use of some older imports has changed, e.g., 'from pythonjsonlogger.jsonlogger import jsonlogger' is incorrect.
- gotcha Ensure that required fields are correctly specified in comma-separated format to avoid formatting issues.
Install
-
pip install python-json-logger
Imports
- JsonFormatter
from pythonjsonlogger.jsonlogger import JsonFormatter
Quickstart
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')