Drop-in structured JSON logging for Python using the stdlib logging module
Project description
philiprehberger-json-logger
Drop-in structured JSON logging for Python using the stdlib logging module.
Installation
pip install philiprehberger-json-logger
Usage
Quick setup
from philiprehberger_json_logger import setup
setup(level="DEBUG")
import logging
logging.info("Server started", extra={"port": 8080})
# {"timestamp": "2026-03-13T12:00:00+00:00", "level": "INFO", "message": "Server started", "logger": "root", "module": "app", "line": 5, "port": 8080}
Named logger
import logging
from philiprehberger_json_logger import setup
logger = logging.getLogger("myapp")
setup(level="INFO", logger=logger)
logger.warning("Disk usage high", extra={"usage_pct": 91.3})
Static extra fields
from philiprehberger_json_logger import setup
setup(level="INFO", extra_fields={"service": "api", "env": "production"})
Every log entry will include "service": "api" and "env": "production".
Field redaction
from philiprehberger_json_logger import setup
setup(level="INFO", redact_fields={"password", "token", "secret"})
import logging
logging.info("Login", extra={"user": "alice", "password": "s3cret"})
# password field will appear as "***"
Redaction works recursively on nested dicts.
Scoped context
import logging
from philiprehberger_json_logger import setup, log_context
setup(level="INFO")
with log_context(request_id="abc-123", user="alice"):
logging.info("Processing request")
# log entry includes request_id and user fields
with log_context(step="validation"):
logging.info("Validating input")
# log entry includes request_id, user, and step fields
Custom handler with JsonFormatter
import logging
from philiprehberger_json_logger import JsonFormatter
handler = logging.FileHandler("app.log")
handler.setFormatter(JsonFormatter(extra_fields={"service": "worker"}, redact_fields={"token"}))
logger = logging.getLogger("worker")
logger.addHandler(handler)
logger.setLevel(logging.INFO)
API
| Name | Description |
|---|---|
JsonFormatter(*, extra_fields=None, redact_fields=None) |
Logging formatter that outputs JSON lines. extra_fields is a dict of static fields merged into every entry. redact_fields is a set of field names replaced with "***". |
setup(level="INFO", *, extra_fields=None, redact_fields=None, logger=None) |
Configure a logger with JSON output. Defaults to the root logger. Clears existing handlers. |
log_context(**kwargs) |
Context manager that injects fields into all log entries within the block. Supports nesting. |
JSON output fields
| Field | Description |
|---|---|
timestamp |
ISO 8601 UTC timestamp |
level |
Log level name (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
message |
Formatted log message |
logger |
Logger name |
module |
Source module name |
line |
Source line number |
exception |
Full traceback string (only present when logging an exception) |
Any extra={} kwargs passed to the log call are merged into the top-level JSON object. Static extra_fields from the formatter and active log_context() fields are also merged.
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider starring the repository.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_json_logger-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_json_logger-0.2.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f2d8e97a43287658a366dd01c11d0e4729de87a2d26075b119ca2e37fabb59a
|
|
| MD5 |
be600815db09956f97250c504f8340da
|
|
| BLAKE2b-256 |
9c9ca4590b0f96d5d9248037fdbd3a34c154f36913ed5555ef91b202ab6b5861
|
File details
Details for the file philiprehberger_json_logger-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_json_logger-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7bb65a992b340e27bb77687d94f5568fa956b5656d44d710cac242dd9bb779d
|
|
| MD5 |
7bc09b1496c2f58f37da14fce1cdbf64
|
|
| BLAKE2b-256 |
deb62a8f74e9e5fa3a96b6fcad214885b26f8dfce20b38298db975f2e019f03e
|