Skip to main content

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.

Release files for context-logger-wrapper 0.0.2

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

Source distribution (sdist)

Source distribution for context-logger-wrapper 0.0.2
File Size Uploaded
context_logger_wrapper-0.0.2.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for context-logger-wrapper 0.0.2
File Interpreter ABI Platform
context_logger_wrapper-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:18.9 kB

Release files / context_logger_wrapper-0.0.2.tar.gz

Download URL context_logger_wrapper-0.0.2.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
837fd8b1da6bda3a4a2adf32552c82d2186274cc8eed1e465863bd4c8697449f
BLAKE2b-256 checksum
How to use checksums
de4759337bca9408cad95567553c3c2be81c35e1ee8e67e348f18374268e31cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / context_logger_wrapper-0.0.2-py2.py3-none-any.whl

Download URL context_logger_wrapper-0.0.2-py2.py3-none-any.whl
Size 5.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
402d146718ba9b3dbee7a47813d1dac1e2af662986c1a06547447de0b9c16987
BLAKE2b-256 checksum
How to use checksums
c33161579e590465fe5d8645a4fa5764b8f925071ed8a6fe8137a105ca15d852
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

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