Install & Compatibility
Where this runs
tested against v1.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.090s · 18MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.076s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
JSONLogFormatter
✓ from json_logging import JSONLogFormatter
✗ from json_logging import init_json_logging
Initializes JSON logging for your application and demonstrates how to log standard messages and messages with extra fields. The `init_json_logging` function configures the root logger (or a specific logger if provided) to use the `JSONLogFormatter`. Messages are output as JSON objects to `stdout` by default.
import logging
import json_logging
import sys
# Initialize JSON logging early in your application lifecycle
json_logging.init_json_logging(enable_json=True)
# Configure a logger
logger = logging.getLogger('my_app_logger')
logger.setLevel(logging.INFO)
# Ensure the handler is using the JSON formatter
# json_logging.init_json_logging typically handles this for the root logger
# or its own default handler. For custom handlers, ensure they use JSONLogFormatter.
# For example:
# handler = logging.StreamHandler(sys.stdout)
# handler.setFormatter(json_logging.JSONLogFormatter())
# logger.addHandler(handler)
logger.info('This is a basic info log message.')
logger.warning('A warning occurred!', extra={'event_id': 'WARN-001', 'user': 'test_user'})
try:
1 / 0
except ZeroDivisionError as e:
logger.error('An error occurred during division.', exc_info=True, extra={'operation': 'math', 'error_type': str(type(e))})
print('\n--- Example Output (trimmed for brevity) ---\n')
# To see output, run this script.
Debug
Known issues
breakingThe import path for `JSONLogFormatter` changed from `json_logging.default_formatter.JSONLogFormatter` to `json_logging.JSONLogFormatter` in version 1.5.0.fixUpdate your imports to `from json_logging import JSONLogFormatter` when upgrading to 1.5.0 or newer.
affects: <1.5.0
gotchaThe `init_json_logging()` function must be called early in your application's lifecycle, before any loggers are used, to ensure all relevant loggers are correctly configured with the JSON formatter. Calling it too late might result in some logs not being formatted as JSON.fixPlace `json_logging.init_json_logging()` at the very beginning of your application's entry point, e.g., `app.py` or `main.py`.
affects: All
deprecatedWhen running with Python 3.12+, older versions of `json-logging` (<1.5.1) may produce deprecation warnings or errors related to `datetime.utcnow()`. This was addressed in version 1.5.1.fixUpgrade to `json-logging` version 1.5.1 or newer to ensure compatibility and avoid `utcnow` related issues.
affects: <1.5.1 on Python 3.12+
gotchaFor full request and response context logging in web frameworks like Flask, FastAPI, or Connexion, `init_json_logging()` alone is often not sufficient. You usually need to integrate specific middleware or call framework-specific helper functions (e.g., `json_logging.util.add_request_and_response_fields` for Flask) to ensure request/response data is captured.fixRefer to the `json-logging` documentation for your specific web framework for detailed integration steps beyond basic initialization.
affects: All
Upgrade
Version history
1.5.1latest on PyPI · released Jul 7, 2025
Audit
Dependencies
flaskoptionalOptional, for Flask web framework integration and request context logging.
fastapioptionalOptional, for FastAPI web framework integration and request context logging.
connexionoptionalOptional, for Connexion web framework integration and request context logging.