Skip to main content

philiprehberger-struct-log

Tests PyPI version License

Structured JSON logging with automatic context propagation.

Installation

pip install philiprehberger-struct-log

Usage

Basic logging

from philiprehberger_struct_log import get_logger

logger = get_logger("myapp")
logger.info("Server started", extra={"port": 8080})
# {"timestamp": "2026-03-21T12:00:00+00:00", "level": "INFO", "message": "Server started", "logger": "myapp", "port": 8080}

Context propagation

from philiprehberger_struct_log import get_logger, bind_context, clear_context

logger = get_logger("myapp")

bind_context(request_id="abc-123", user="alice")
logger.info("Processing request")
# {"timestamp": "...", "level": "INFO", "message": "Processing request", "logger": "myapp", "request_id": "abc-123", "user": "alice"}

clear_context()

Inspecting context

from philiprehberger_struct_log import bind_context, get_context

bind_context(service="api", env="production")
ctx = get_context()
# {"service": "api", "env": "production"}

API

Name Description
get_logger(name) Get or create a logger with a StructHandler attached. Level is set to DEBUG.
bind_context(**kwargs) Store key-value pairs in thread-local context. Included in every log entry on the current thread.
clear_context() Clear all bound context for the current thread.
get_context() Return a copy of the current thread-local context as a dict.
StructHandler(stream=None) Logging handler that formats records as JSON lines. Defaults to stderr.

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

Bound context fields and any extra={} kwargs passed to the log call are merged into the top-level JSON object.

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_struct_log-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_struct_log-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_struct_log-0.1.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_struct_log-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6c5cd9a1783e5ffe55340cabb358c15d849961b9a8697372c09e86279defe598
MD5 17fb2331a1613c51eed774837d334e6b
BLAKE2b-256 faf1f569c8ead5bad57e0e8e8223bd90b68d697b906763abc53fe54093366896

See more details on using hashes here.

File details

Details for the file philiprehberger_struct_log-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_struct_log-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 016a4aa3beebcd4dbe11e051fc79714715790b786252f27ea394e78fc4d65a47
MD5 2690cbd2d67736e2d1ba733356d97a9c
BLAKE2b-256 240a53afe9b1abb1bd5ec8769bad79ae5ecbfa28fa40ce28a3818bcb420b8f39

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 Sentry Error logging StatusPage Status page