Skip to main content

philiprehberger-struct-log

Tests PyPI version Last updated

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()

Scoped context

Use log_context as a context manager for nested scoped context. On exit, the previous context is automatically restored.

from philiprehberger_struct_log import get_logger, log_context

logger = get_logger("myapp")

with log_context(request_id="abc"):
    logger.info("handling")  # includes request_id
    with log_context(user_id="123"):
        logger.info("auth")  # includes request_id AND user_id
    logger.info("done")  # only request_id

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.
log_context(**kwargs) Context manager that merges kwargs into context on enter and restores previous context on exit. Supports nesting.
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

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

Metadata

Release files for philiprehberger-struct-log 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-struct-log 0.2.1
File Size Uploaded Provenance Actions
philiprehberger_struct_log-0.2.1.tar.gz 6.7 kB Without provenance Details for philiprehberger_struct_log-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-struct-log 0.2.1
File Interpreter ABI Platform Actions
philiprehberger_struct_log-0.2.1-py3-none-any.whl Python 3 none any Details for philiprehberger_struct_log-0.2.1-py3-none-any.whl

Total release size: 11.9 kB

Release files / philiprehberger_struct_log-0.2.1.tar.gz

Download URL philiprehberger_struct_log-0.2.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
897aba8d9ecd68716222234a88a506addb730cf284add0d28b5f669d106473cd
BLAKE2b-256 checksum
How to use checksums
1932a5bf98749ca1450f6d69482492a0fa0655fe4d6117f747021001f2b0be76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_struct_log-0.2.1-py3-none-any.whl

Download URL philiprehberger_struct_log-0.2.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d51111969b8676ddd17e8c8f9d07d30cccb29b07de42c49a9fc06517c8b88a4
BLAKE2b-256 checksum
How to use checksums
3c85533553fa51485f12452d37fd35832806ede0d0fd7b51563595406931aa95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for philiprehberger-struct-log 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page