Skip to main content

Drop-in structured JSON logging for Python using the stdlib logging module

Project description

philiprehberger-json-logger

Tests PyPI version Last updated

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 project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_json_logger-0.2.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_json_logger-0.2.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_json_logger-0.2.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_json_logger-0.2.1.tar.gz
Algorithm Hash digest
SHA256 de4fc9067fc5f08f8aa53ce1f6ef741761af87dad08d5b13963e050d35458400
MD5 afadadc3a7e2b696e03b812e0a688d34
BLAKE2b-256 146a28e33a948cdb10746a0bcfb69b153053f218024f8f761bbd69a203216fc5

See more details on using hashes here.

File details

Details for the file philiprehberger_json_logger-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_json_logger-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79af4f49878b618ba273243b8fa2b2919cc1dbae32a8c6e9c23153838c3cf14f
MD5 fceb8edefd10b8ec93870f2eef229e6e
BLAKE2b-256 68d915367c0e771b3ff99b9c47833f0f2655dca3fb9d78c3e3923739e714c253

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page