https://github.com/aviramha/contextfilter
Easy contextual information logging filter, using ContextVars.
https://github.com/aviramha/contextfilter
Last synced: about 2 months ago
JSON representation
Easy contextual information logging filter, using ContextVars.
- Host: GitHub
- URL: https://github.com/aviramha/contextfilter
- Owner: aviramha
- License: mit
- Created: 2020-08-12T14:25:15.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-14T06:23:42.000Z (almost 5 years ago)
- Last Synced: 2025-03-25T12:22:26.886Z (2 months ago)
- Language: Python
- Size: 18.6 KB
- Stars: 10
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# contextfilter


Small, helper library for logging contextual information using contextvars in Python 3.7.
## Installation
Using pip
```
$ pip install contextfilter
```## Usage
```py
import logging
from contextvars import ContextVar
from contextfilter import ContextVarFilter, ConstContextFilterrequest_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 3cf = 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](https://poetry.eustace.io):
```sh
$ git clone [email protected]:{USER}/contextfilter.git
$ make install
```To run tests and linters use command below:
```sh
$ 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.:
```sh
$ 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` to `ContextVarFilter` - 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