简体   繁体   English

如何从非 root 记录器记录 INFO 消息?

[英]How to log INFO messages from the non-root logger?

I know that default level for root logger is warning (30).我知道根记录器的默认级别是警告(30)。 If I create a non-root logger and if there is no handler defined on that, then the logger will use the level of the root logger (and handler of the root logger).如果我创建了一个非根记录器并且没有在其上定义处理程序,那么记录器将使用根记录器的级别(以及根记录器的处理程序)。 What is the handler level for the root handler?根处理程序的处理程序级别是什么?

#assume no root logger is configured and below code gets the non-root logger for the module
logger = logging.getLogger(__name__)
#get effective level - this is inherited from the root logger
print(logger.getEffectiveLevel())
#set the level for this logger to 10
logger.setLevel(10)
#print level which shows 10
print(logger.getEffectiveLevel())
logger.info('this does not get logged')
logger.warning('this gets logged')

How can I get info to print?如何获取要打印的信息?

The logger doesn't have any handlers explicitly attached to it by default:默认情况下,记录器没有显式附加任何处理程序:

>>> logger.handlers
[]

However, in the .callHandlers() method , the logging module will actually fall back to a lastResort handler that has a level of WARNING :但是,在.callHandlers()方法中,日志模块实际上会退回到具有WARNING级别的lastResort处理程序:

_defaultLastResort = _StderrHandler(WARNING)
lastResort = _defaultLastResort

# ....

    if (found == 0):
        if lastResort:
            if record.levelno >= lastResort.level:
                lastResort.handle(record)

As indicated in the comments, attaching a handler to logger , either viaa basicConfig() or explicitly through addHandler() , and then setting the level of that handler, will mean the logger doesn't fall back to lastResort .如评论中所述,通过basicConfig()或显式通过 addHandler( addHandler()将处理程序附加到logger ,然后设置该处理程序的级别,将意味着 logger 不会回lastResort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM