Skip to main content

Context Logger Wrapper

Project description

Context Logger Wrapper

CI

context_logger_wrapper is a small Python package for adding scoped context to standard logging output, including messages emitted through warnings.warn.

It wraps a normal logging.Logger with log-with-context support while keeping the underlying logger's attributes and methods available through passthroughs. That lets existing logging setup keep using handlers, propagation, filters, and formatters while application code adds contextual fields around blocks of work.

Why It Exists

Python warnings can be routed into logging with logging.captureWarnings(True), but the resulting py.warnings logger is just a regular logger. This package adds a wrapper and a warning hook so warnings can receive the same context as normal log calls.

The context is attached through logging extra fields. To see those fields in output, configure a formatter that renders them, such as a JSON formatter that includes extra record attributes.

Installation

pip install context_logger_wrapper

The package requires Python 3.8 or newer and depends on log-with-context and extendanything.

For local development:

pip install -r requirements_dev.txt
pip install -e .

Usage

import logging
import warnings

from context_logger_wrapper import (
    ContextLoggerWrapper,
    capture_warnings_with_context,
)
from log_with_context import add_logging_context

logger = ContextLoggerWrapper(name=__name__)

# Equivalent when you already have a logger:
# logger = ContextLoggerWrapper(logger=logging.getLogger(__name__))

# Optional: route warnings.warn(...) through a context-aware py.warnings logger.
capture_warnings_with_context()

with add_logging_context(request_id="abc123"):
    logger.info("started")

    with add_logging_context(step="load-data"):
        logger.info("running step", extra={"attempt": 1})
        warnings.warn("example warning")

How It Works

ContextLoggerWrapper subclasses log_with_context.Logger and initializes it with either a logger name or an existing logging.Logger. It also stores the wrapped base logger in ExtendAnything, so unknown attributes are forwarded to the underlying logger.

capture_warnings_with_context() replaces warnings.showwarning with a hook that formats warnings and logs them through a wrapped logging.getLogger("py.warnings"). If a warning is explicitly directed to a file object, the hook delegates back to the original warnings.showwarning.

Nested add_logging_context(...) blocks accumulate context for log records created inside them.

Behavior and Limitations

  • The package does not install or configure logging handlers or formatters.
  • Warning capture changes process-global warning behavior by replacing warnings.showwarning.
  • The source notes successful use with joblib.Parallel multiprocessing and threading backends, with mixed results for the loky backend.

Development

make test
make lint
make docs

The repository also includes Sphinx documentation configuration and CI settings for tests, linting, package publishing, and optional docs deployment.

Changelog

0.0.1

  • First release on PyPI.

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

context_logger_wrapper-0.0.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

context_logger_wrapper-0.0.2-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file context_logger_wrapper-0.0.2.tar.gz.

File metadata

  • Download URL: context_logger_wrapper-0.0.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for context_logger_wrapper-0.0.2.tar.gz
Algorithm Hash digest
SHA256 837fd8b1da6bda3a4a2adf32552c82d2186274cc8eed1e465863bd4c8697449f
MD5 154138cf412ca2dc0fe6a7100f267082
BLAKE2b-256 de4759337bca9408cad95567553c3c2be81c35e1ee8e67e348f18374268e31cd

See more details on using hashes here.

File details

Details for the file context_logger_wrapper-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for context_logger_wrapper-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 402d146718ba9b3dbee7a47813d1dac1e2af662986c1a06547447de0b9c16987
MD5 7e90e9dacf4ccd1558ab638e363eae6b
BLAKE2b-256 c33161579e590465fe5d8645a4fa5764b8f925071ed8a6fe8137a105ca15d852

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