跳转到主要内容

用于轻松丰富日志记录的ContextVars过滤器

项目描述

contextfilter

Version License Tests

Python 3.7中使用contextvars进行日志记录的小型辅助库。

安装

使用pip

$ pip install contextfilter

用法

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.

贡献

要在contextfilter代码库上工作,您需要将该项目分叉并本地克隆它,并通过poetry安装所需的依赖项

$ git clone git@github.com:{USER}/contextfilter.git
$ make install

要运行测试和linters,请使用以下命令

$ make lint && make test

如果您只想运行测试或linters,您可以明确指定要运行的测试环境,例如

$ make lint-black

许可

contextfilter在MIT许可下授权。有关详细信息,请参阅许可文件。

最新更改

0.3.0 (2020-8-14)

  • ContextFilter重命名为ContextVarFilter - 重构API - 现在接受由调用者创建的ContextVars。由@bentheiii提供的设计建议
  • 添加了ConstContextFilter,它向日志记录添加了常量属性。
  • 修复了#5

项目详情


下载文件

下载适合您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装软件包的信息。

源分布

contextfilter-0.3.0.tar.gz (3.5 kB 查看散列)

上传时间:

构建分发

contextfilter-0.3.0-py3-none-any.whl (3.7 kB 查看哈希值)

上传时间: Python 3

支持