python的映射诊断上下文(mdc)库
mdc的Python项目详细描述
这被认为是一个易于使用的、导入和运行的、用于映射诊断上下文样式日志记录的库。
日志应该包括python日志库收集的所有必需字段。 在需要的地方提供帮助器方法来添加上下文字段。
安装
PYPI:
$ pip install --user mdc
来源:
$ pip install --user .
用法
向根日志记录器添加处理程序,或使用logging.basicconfig设置基处理程序:
>>>importlogging>>>frommdcimportMDCHandler>>># use the MDCHandler only>>>logging.basicConfig(level=logging.DEBUG,handlers=[MDCHandler()])>>># the MDC contextmanager>>>withMDC(foo='bar'):...logging.warning('foobar'){...,"mdc":{"foo":"bar"},"extra":{},...}>>># the with_mdc decorator>>>@with_mdc(test='123')...deffoobar(ctx):...logging.warning('some warning')>>>foobar(){...,"mdc":{"test":"123"},"extra":{},...}>>># logging with extra fields>>>logging.error('some error',extra=dict(foo='bar')){...,"mdc":{},"extra":{"foo":"bar"},...}
默认情况下,日志消息将包括以下字段:
{"message":"deleting context b8321b4f-19ff-4c98-b011-5a97178e7ad6","logger":"mdc","timestamp":"2018-03-07T21:39:12.010851","level":"DEBUG","mdc":{"foo":"bar","index":76},"extra":{},"python":{"module":"__init__","function":"MDC","path":"/home/user/git/mdc/mdc/__init__.py","file":"__init__.py","line":56,"process":{"name":"MainProcess","id":31678},"thread":{"name":"MainThread","id":140433289192768}}}
运行测试:
$ tox