方便记录当前上下文信息的工具
context-logging的Python项目详细描述
上下文记录
说明
方便记录当前上下文信息的工具
安装
pip install context_logging
用法
作为ContextManager
fromcontext_loggingimportContext,current_contextwithContext(val=1):assertcurrent_context['val']==1assert'val'notincurrent_context
允许任何上下文嵌套
withContext(val=1):assertcurrent_context=={'val':1}withContext(val=2,var=2):assertcurrent_context=={'val':2,'var':2}assertcurrent_context=={'val':1}assert'val'notincurrent_context
作为装潢师
@Context(val=1)deff():assertcurrent_context['val']==1f()assert'val'notincurrent_context
开始/结束
ctx=Context(val=1)assert'val'notincurrent_contextctx.start()assertcurrent_context['val']==1ctx.finish()assert'val'notincurrent_context
写入/删除当前上下文
withContext():assert'val'notincurrent_contextcurrent_context['val']=1assertcurrent_context['val']==1
显式上下文名称(否则将使用python模块的路径)
withContext(name='my_context'):pass
设置记录上下文
importloggingfromcontext_loggingimportcurrent_context,setup_log_recordlogging.basicConfig(format='%(asctime)s%(levelname)s%(name)s%(message)s%(context)s',level=logging.INFO)setup_log_record()current_context['val']=1logging.info('message')# 2019-07-25 19:49:43,892 INFO root message {'val': 1}
从上下文退出时执行的日志记录时间(用{{withContext(name='my_context'):time.sleep(1)# INFO 'my_context: executed in 00:00:01',
上下文中的异常用当前的上下文disable填充,fill_exception_context=False
try:withContext(val=1):raiseException(1)exceptExceptionasexc:assertexc.args=(1,{'val':1})
我们可以将数据设置为永远不会关闭的根上下文
fromcontext_loggingimportroot_contextroot_context['env']='test'
用于在异步代码中自动填充线程上下文
fromcontextvars_executorimportContextVarExecutorloop.set_default_executor(ContextVarExecutor())
对于开发人员
创建venv并安装deps
make init
安装Git预调试挂钩
make precommit_install
运行linters、autoformat、测试等
make pretty lint test
bump新版本
make bump_major
make bump_minor
make bump_patch
许可证
麻省理工学院
更改日志
未发布
- 。
1.0.0-2019年7月29日
- 日志中没有显示空上下文
0.2.0-2019年7月25日
- 作为日志记录属性的上下文
0.1.0-2019年7月23日
- 首字母
推荐PyPI第三方库
withContext(name='my_context'):time.sleep(1)# INFO 'my_context: executed in 00:00:01',
上下文中的异常用当前的上下文disable填充,fill_exception_context=False
try:withContext(val=1):raiseException(1)exceptExceptionasexc:assertexc.args=(1,{'val':1})
我们可以将数据设置为永远不会关闭的根上下文
fromcontext_loggingimportroot_contextroot_context['env']='test'
用于在异步代码中自动填充线程上下文
fromcontextvars_executorimportContextVarExecutorloop.set_default_executor(ContextVarExecutor())
对于开发人员
创建venv并安装deps
make init
安装Git预调试挂钩
make precommit_install
运行linters、autoformat、测试等
make pretty lint test
bump新版本
make bump_major
make bump_minor
make bump_patch
许可证
麻省理工学院
更改日志
未发布
- 。
1.0.0-2019年7月29日
- 日志中没有显示空上下文
0.2.0-2019年7月25日
- 作为日志记录属性的上下文
0.1.0-2019年7月23日
- 首字母