即使我将级别设置为INFO,Python日志记录根记录器也不会显示信息
我创建了以下脚本.你们中的任何人都可以向我解释为什么输出如下所示
I created the following script. Could any of you explain to me why the output is like what shows below
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
print('debug', logger.isEnabledFor(logging.DEBUG))
print('info', logger.isEnabledFor(logging.INFO))
print('warning', logger.isEnabledFor(logging.WARNING))
print('error', logger.isEnabledFor(logging.ERROR))
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
输出
debug True
info True
warning True
error True
warning
error
DEBUG:root:debug
INFO:root:info
WARNING:root:warning
ERROR:root:error
特别是
-
此处
logger.info
和logging.info
有什么区别
为什么logger.isEnabledFor(logging.DEBUG)
是True
而logger.debug('debug')
没有显示任何内容
how come that logger.isEnabledFor(logging.DEBUG)
is True
while logger.debug('debug')
does not show anything
logger.info
没有输出但logging.info
有输出
需要澄清的几件事:
- root logger的默认日志级别为
WARNING
-
如果不执行任何操作(即未设置任何处理程序或格式化程序),则不会初始化Root logger:
- Default log level for root logger is
WARNING
Root logger is not initialized if you do nothing, that is, without any handlers or formatter set up:
>>> import logging
>>> logging.root.handlers
[]
好的,但是您发现了问题:将日志记录级别设置为DEBUG
时,根记录器无法正常工作.调试消息将被忽略.对于未配置的相同根记录器,警告消息将正常输出.为什么会这样?
Okay, but you found out the problem: when logging level set to DEBUG
, the root logger is not working as expected. Debug messages are ignored. With the same not configured root logger, warning messages output normally. Why is that?
请记住,我们目前没有任何用于root记录程序的处理程序.但是查看代码,我们确实看到了:
Keep in mind we don't have any handler for root logger right now. But looking into the code, we do see:
if (found == 0):
if lastResort:
if record.levelno >= lastResort.level:
lastResort.handle(record)
elif raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = True
这意味着,如果未找到任何处理程序,我们将使用lastResort
作为备份.您可以参考lastResort
的定义,它以日志记录级别WARNING
初始化.同时,调试消息没有备份,因此在未设置处理程序时将忽略它们.
Which means, we have a lastResort
for backup if no handler is found. You can refer to the definition of lastResort
, it is initialized with logging level WARNING
. Meanwhile, debug messages don't have this backup so they are ignored when no handler is set.
对于您的问题:
- 这两个记录器是相同的,因为当
getLogger()
不接收任何参数时,将返回根记录器. - 请参见下文:
- These two loggers are identical, since the root logger is returned when
getLogger()
receives no arguments. - See below:
Logger.isEnabledFor(lvl)
Logger.isEnabledFor(lvl)
指示严重性为lvl的消息是否会 由该记录器处理.此方法首先检查模块级别 通过logging.disable(lvl)设置的级别,然后记录器的有效级别 级别由getEffectiveLevel()确定.
Indicates if a message of severity lvl would be processed by this logger. This method checks first the module-level level set by logging.disable(lvl) and then the logger’s effective level as determined by getEffectiveLevel().
logging
模块中的任何日志记录功能都将使用basicConfig()
初始化根记录器,从而添加一个默认处理程序,以便随后在logger
上进行的调用也将起作用.
logging
module will initialize the root logger with basicConfig()
which adds a default handler, so that the subsequent calls on logger
will also work.您应该做的是,使用logging.basicConfig()
为根记录程序设置默认处理程序,然后根据记录程序级别和消息级别输出消息.
What you should do is, use logging.basicConfig()
to set up a default handler for root logger and messages will be output according to the logger level and message level.