使用 python 标准日志记录模块,可以使用以下命令添加原始日志调用的行号:%(lineno)s.
这如何使用 structlog 来完成?
我有类似的需求,我最终创建了一个自定义处理器
我查看了当structlog
它被告知“假装”以与库兼容的模式格式化时输出模块和行号的内容logging
(意思是:当它使用常规时stdlib.LoggerFactory
),我从中找到了灵感。关键是以下几句话……
通过使用 structlog 的 structlog.stdlib.LoggerFactory,还可以确保函数名称和行号等变量在您的日志格式中正确展开。
...来自此文档页面
代码似乎一直在寻找执行帧,直到找到与日志无关的模块中的一个。
我structlog
在一个名为的模块内进行了所有设置,my_libs.util.logger
因此我想获取不在该模块内的第一帧。为了做到这一点,我告诉它将我的日志记录添加my_libs.util.logger
到这些排除项中。这就是additional_ignores
下面代码中的作用。
在示例'my_libs.util.logger'
中,为了清楚起见,我在排除列表中硬编码了模块的名称 ( ),但如果您有类似的设置,您可能最好__name__
改用它。这样做是忽略由于日志机制到位而存在的执行帧。您可以将其视为一种忽略在实际记录消息过程中可能发生的调用的方式。或者,换句话说,在您想要输出的实际模块/行中发生的调用之后发生的调用。logging.info("Foo")
一旦找到正确的框架,提取任何类型的信息(模块名称、函数名称、行号...)都非常容易,尤其是使用检查模块。我选择输出模块名称和行号,但可以添加更多字段。
# file my_libs/util/logger.py
import inspect
from structlog._frames import _find_first_app_frame_and_name
def show_module_info_processor(logger, _, event_dict):
# If by any chance the record already contains a `modline` key,
# (very rare) move that into a 'modline_original' key
if 'modline' in event_dict:
event_dict['modline_original'] = event_dict['modline']
f, name = _find_first_app_frame_and_name(additional_ignores=[
"logging",
'my_libs.util.logger', # could just be __name__
])
if not f:
return event_dict
frameinfo = inspect.getframeinfo(f)
if not frameinfo:
return event_dict
module = inspect.getmodule(f)
if not module:
return event_dict
if frameinfo and module:
# The `if` above is probably redundant, since we already
# checked for frameinfo and module but... eh... paranoia.
event_dict['modline'] = '{}:{}'.format(
module.__name__,
frameinfo.lineno,
)
return event_dict
def setup_structlog(env=None):
# . . .
ch.setFormatter(logging.Formatter('%(message)s'))
logging.getLogger().handlers = [ch]
processors = [
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
# . . . more . . .
show_module_info_processor, # THIS!!!
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
structlog.processors.format_exc_info,
structlog.processors.StackInfoRenderer(),
# . . . more . . .
]
# . . . more . . .
structlog.configure_once(
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
context_class=structlog.threadlocal.wrap_dict(dict),
processors=processors,
)
这会产生如下输出:
server_1
| INFO [my_libs.hdfs] 2019-07-01 01:01:01 [info ] Initialized HDFS
[my_libs.hdfs] modline=my_libs.hdfs:31
看看这个关于如何获取行号的更一般问题的答案。 https://stackoverflow.com/a/3056270/5909155 这不能使用 log.bind(...) 绑定到记录器,因为每次登录时都必须对其进行评估。因此,您应该像这样添加一个键值对
logger.log(..., lineno=inspect.getframeinfo(inspect.currentframe()).lineno)
每一次。不过,也许可以将其包装在一个函数中,如下所示:https ://stackoverflow.com/a/20372465/5909155 不要忘记
import inspect