log-with-context–a thread-local, context-preserving Python logger
log-with-context is a Python logger that saves variables in a thread-local context to be passed as extra to Python logging methods.
Note that log-with-context assumes that you are passing context in a multithreading sense–not in an asyncio sense.
Installation
This library is available on PyPI and can be installed with
python3 -m pip install log-with-context
Usage
This library provides a wrapped Python logging.Logger that adds a shared context to each logging message, passed as the extra parameter.
You will need an additional library (like JSON-log-formatter) to actually output the logging messages. We avoided putting this functionality in this library to keep it lightweight and flexible. We assumed that you already have a preferred way to format your logging messages.
import logging
import logging.config
from log_with_context import add_logging_context, Logger
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"json": {"()": "json_log_formatter.JSONFormatter"},
},
"handlers": {
"console": {
"formatter": "json",
"class": "logging.StreamHandler",
}
},
"loggers": {
"": {"handlers": ["console"], "level": "INFO"},
},
})
LOGGER = Logger(__name__)
LOGGER.info("First message. No context")
with add_logging_context(current_request="hi"):
LOGGER.info("Level 1")
with add_logging_context(more_info="this"):
LOGGER.warning("Level 2")
LOGGER.info("Back to level 1")
LOGGER.error("No context at all...")
The above program logs the following messages to standard error:
{"message": "First message. No context", "time": "2021-04-08T16:37:23.126099"}
{"current_request": "hi", "message": "Level 1", "time": "2021-04-08T16:37:23.126336"}
{"current_request": "hi", "more_info": "this", "message": "Level 2", "time": "2021-04-08T16:37:23.126389"}
{"current_request": "hi", "message": "Back to level 1", "time": "2021-04-08T16:37:23.126457"}
{"message": "No context at all...", "time": "2021-04-08T16:37:23.126514"}
This example may look trivial, but it is very handy to maintain a logging context up and down a Python call stack without having to pass additional variables to the functions and methods that you call.
Implementation details
Logging contexts are stored as thread-local variables. If you want to share information between threads, you must create a Logging context in each thread with the same information.
Similarly, logging contexts are deliberately not copied when creating subprocesses. This is done to minimize bugs and make sure that log-with-context behaves in the exact same manner across operating systems.
Metadata
Release files for log-with-context 0.6.0
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 | |
|---|---|---|---|---|
| log_with_context-0.6.0.tar.gz | 4.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| log_with_context-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.4 kB
Release files / log_with_context-0.6.0.tar.gz
| Download URL | log_with_context-0.6.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2bfedce2b13d2a2da4ef5162b5b7ba1d6d66e3a2bddb0df54603e01f67e7a18f
|
|
BLAKE2b-256 checksum How to use checksums |
4b1a300dc5025e84cac830bb20aab4fee453da85247b434e34998da2362ad727
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.9.18 Linux/5.15.0-1049-azure
|
Release files / log_with_context-0.6.0-py3-none-any.whl
| Download URL | log_with_context-0.6.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a79442598033b58deabbfd046217584a5263f9a81dd9140224c9ad4db6abb881
|
|
BLAKE2b-256 checksum How to use checksums |
10b62764207234ab77e1ae222642a3fc52a49ab3f172d83d9f0f04ca77b081d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.9.18 Linux/5.15.0-1049-azure
|
Security for log-with-context 0.6.0
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.