contextuallogging
Pure-Python semantic logging library with context-local values.
Installation
This package is available on PyPI and can be installed with pip:
pip install contextuallogging
Basic Usage
This packages introduces two primary components:
- The
ContextualFormatterlogging formatter subclass. - The
@contextdecorator.
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.
Metadata
Release files for contextuallogging 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| contextuallogging-1.0.3.tar.gz | 9.4 kB | Without provenance | Details for contextuallogging-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| contextuallogging-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for contextuallogging-1.0.3-py3-none-any.whl |
Total release size: 18.4 kB
Release files / contextuallogging-1.0.3.tar.gz
| Download URL | contextuallogging-1.0.3.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0c982a4da37fcce917df98559add4b33facd7b82a04e7c427da1ff121601b5cb
|
|
BLAKE2b-256 checksum How to use checksums |
600c2cb475c0be4ae22e33852270096fdded765f989275206d21f514a2be676d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / contextuallogging-1.0.3-py3-none-any.whl
| Download URL | contextuallogging-1.0.3-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b2cf6b99e56e6b7cb6dc560ab732b0c7ff4cabb340aa23e2ea5ff44ca0bbdf1
|
|
BLAKE2b-256 checksum How to use checksums |
5e5a645f8ae2dfcc6034ae6a5c378ea66d3272dbc3b6a2c31be092e643de67c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for contextuallogging 1.0.3
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.