gpt4 book ai didi

python - LogRecord 没有预期的字段

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:16 24 4
gpt4 key购买 nike

在使用“日志记录”模块的 Python 中,文档 promise LogRecord 实例将具有许多属性,这些属性在文档中明确列出。

然而,这似乎并不总是正确的。当我不使用日志记录模块的“basicConfig()”方法时,下面的程序显示属性“asctime”和“message”不存在于传递给 LogHandler 的“emit”方法的 LogRecords 中。

import logging

class LoggingHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
assert isinstance(record, logging.LogRecord)
print("LoggingHandler received LogRecord: {}".format(record))

# List of LogRecord attributes expected when reading the
# documentation of the logging module:

expected_attributes = \
"args,asctime,created,exc_info,filename,funcName,levelname," \
"levelno,lineno,module,msecs,message,msg,name,pathname," \
"process,processName,relativeCreated,stack_info,thread,threadName"

for ea in expected_attributes.split(","):
if not hasattr(record, ea):
print("UNEXPECTED: LogRecord does not have the '{}' field!".format(ea))


loggingHandler = LoggingHandler()
rootLogger = logging.getLogger()
rootLogger.addHandler(loggingHandler)

# emit an WARNING message
logging.warning("WARNING MESSAGE")

在 Python 3 上运行它会得到:

$python3 test_logging.py
LoggingHandler received LogRecord: <LogRecord: root, 30, test_logging.py, 28, "WARNING MESSAGE">
UNEXPECTED: LogRecord does not have the 'asctime' field!
UNEXPECTED: LogRecord does not have the 'message' field!

这是怎么回事?我误解了文档吗?需要做什么来确保 LogRecord 实例具有 promise 的“asctime”和“message”属性?

最佳答案

这是Formatter的责任设置 asctimemessage 所以在调用 self.format(record) 之前,这些属性是未定义的。来自 format 的文档方法:

The record’s attribute dictionary is used as the operand to a string formatting operation. Returns the resulting string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using msg % args. If the formatting string contains '(asctime)', formatTime() is called to format the event time.

由于您的示例代码不调用 self.format(record),因此这些属性未定义是预期的行为。

要设置messageasctime,您必须首先在emit中调用self.format(record) > 方法。请尝试

import logging

class LoggingHandler(logging.Handler):
def emit(self, record):
assert isinstance(record, logging.LogRecord)
print("LoggingHandler received LogRecord: {}".format(record))

self.format(record)

# List of LogRecord attributes expected when reading the
# documentation of the logging module:

expected_attributes = \
"args,asctime,created,exc_info,filename,funcName,levelname," \
"levelno,lineno,module,msecs,message,msg,name,pathname," \
"process,processName,relativeCreated,stack_info,thread,threadName"

for ea in expected_attributes.split(","):
if not hasattr(record, ea):
print("UNEXPECTED: LogRecord does not have the '{}' field!".format(ea))


formatter = logging.Formatter("%(asctime)s")
loggingHandler = LoggingHandler()
loggingHandler.setFormatter(formatter)
rootLogger = logging.getLogger()
rootLogger.addHandler(loggingHandler)

# emit an WARNING message
logging.warning("WARNING MESSAGE")

关于python - LogRecord 没有预期的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com