Skip to main content

Pure-Python semantic logging library with context-local values.

Project description

contextuallogging

Stable Version Build Status Documentation Status Checked with mypy Linting: Ruff

Pure-Python semantic logging library with context-local values.

PyPI
Source
Documentation

Installation

This package is available on PyPI and can be installed with pip:

pip install contextuallogging

Basic Usage

This packages introduces two primary components:

  1. The ContextualFormatter logging formatter subclass.
  2. The @context decorator.

This is a basic example:

import sys
import logging
from contextuallogging import context, ContextualFormatter

logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(ContextualFormatter())
logger.addHandler(handler)
logger.setLevel(logging.INFO)

@context(keyword="username", key="user")
def inner_function(username: str) -> None:
    logger.info("inner_function")

@context(keyword="request_id")
def outer_function(request_id: str) -> None:
    logger.info("outer_function")
    inner_function(username="cfandrews")
    logger.info("outer_function again")

outer_function(request_id="7fb9b341")

Which will output:

{"level": "INFO", "logger": "root", "message": "outer_function", "request_id": "7fb9b341", "timestamp": "2023-11-25T20:56:41.796564Z"}
{"level": "INFO", "logger": "root", "message": "inner_function", "request_id": "7fb9b341", "timestamp": "2023-11-25T20:56:41.797024Z", "user": "cfandrews"}
{"level": "INFO", "logger": "root", "message": "outer_function again", "request_id": "7fb9b341", "timestamp": "2023-11-25T20:56:41.797075Z"}

ContextualFormatter

The ContextualFormatter is a subclass of logging.Formatter and can be used in the exact same way as any other logging formatter. This class formats log messages as JSON blobs and includes a number of fields by default, such as an ISO 8601 timestamp in the UTC timezone, log level, logger name, and message. Logging context is added to the output JSON blob as additional fields mixed into the base log.

@context

The @context decorator adds fields to the logging context from the keyword arguments passed into the wrapped function. The logging context persists for the entire function call and is automatically reset after the function returns, creating a stack-like structure in which context is persisted down the call stack but not back up.

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

contextuallogging-1.0.3.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

contextuallogging-1.0.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file contextuallogging-1.0.3.tar.gz.

File metadata

  • Download URL: contextuallogging-1.0.3.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for contextuallogging-1.0.3.tar.gz
Algorithm Hash digest
SHA256 0c982a4da37fcce917df98559add4b33facd7b82a04e7c427da1ff121601b5cb
MD5 8127f355d7ca5fdd5785643ef5b09e6b
BLAKE2b-256 600c2cb475c0be4ae22e33852270096fdded765f989275206d21f514a2be676d

See more details on using hashes here.

File details

Details for the file contextuallogging-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for contextuallogging-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9b2cf6b99e56e6b7cb6dc560ab732b0c7ff4cabb340aa23e2ea5ff44ca0bbdf1
MD5 1e431493e77a8b36288bc071ed831a07
BLAKE2b-256 5e5a645f8ae2dfcc6034ae6a5c378ea66d3272dbc3b6a2c31be092e643de67c0

See more details on using hashes here.

Supported by

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