即使我将级别设置为INFO,Python日志记录根记录器也不会显示信息 [英] python logging root logger does not show info even if I set the level to INFO

查看:452
本文介绍了即使我将级别设置为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

特别是

  1. 此处logger.infologging.info有什么区别

为什么logger.isEnabledFor(logging.DEBUG)Truelogger.debug('debug')没有显示任何内容

how come that logger.isEnabledFor(logging.DEBUG) is True while logger.debug('debug') does not show anything

logger.info没有输出但logging.info有输出

推荐答案

需要澄清的几件事:

  1. root logger的默认日志级别为WARNING
  2. 如果不执行任何操作(即未设置任何处理程序或格式化程序),则不会初始化Root logger:

  1. Default log level for root logger is WARNING
  2. 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.

对于您的问题:

  1. 这两个记录器是相同的,因为当getLogger()不接收任何参数时,将返回根记录器.
  2. 请参见下文:
  1. These two loggers are identical, since the root logger is returned when getLogger() receives no arguments.
  2. 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上进行的调用也将起作用.
  • Calling any logging functions in 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.

    这篇关于即使我将级别设置为INFO,Python日志记录根记录器也不会显示信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆