ContextVars Filter for easily enriching log records
Project description
contextfilter
Small, helper library for logging contextual information using contextvars in Python 3.7.
Installation
Using pip
$ pip install contextfilter
Usage
import logging
from contextvars import ContextVar
from contextfilter import ContextVarFilter, ConstContextFilter
request_id: ContextVar[int] = ContextVar('request_id')
logger = logging.getLogger("test")
cf = ContextFilter(request_id=request_id)
request_id.set(3)
logger.addFilter(cf)
logger.info("test")
# Log record will contain the attribute request_id with value 3
cf = ConstContextFilter(some_const=1)
logger.addFilter(cf)
logger.info("test")
# Log record will contain the attribute some_const with value 1.
Contributing
To work on the contextfilter
codebase, you'll want to fork the project and clone it locally and install the required dependencies via poetry:
$ git clone git@github.com:{USER}/contextfilter.git
$ make install
To run tests and linters use command below:
$ make lint && make test
If you want to run only tests or linters you can explicitly specify which test environment you want to run, e.g.:
$ make lint-black
License
contextfilter
is licensed under the MIT license. See the license file for details.
Latest changes
0.3.0 (2020-8-14)
- Renamed
ContextFilter
toContextVarFilter
- Revamped the API - It now accepts ContextVars created by caller. Suggestion for design by @bentheiii - Added
ConstContextFilter
which adds constant attributes to the log record. - Fixed #5
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
contextfilter-0.3.0.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for contextfilter-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c68c4cf58a62230e27c13ab2717b2d7e93508934b62dce59661dbaada8261ec |
|
MD5 | 5eb0f922930a6e21b513d44627e7aa06 |
|
BLAKE2b-256 | 5d81f95580d147c9aa87228b3535f6a6f6f28f39c55560ba51ebc7f76d54604f |