gpt4 book ai didi

python - 子记录器的默认处理程序是什么?

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

我知道根记录器具有 lastResort 处理程序,如果未使用 logging.basicConfig 配置根记录器处理程序并且用户使用logging.info/debug/warning/error/ritic("some msg")或者 logger =logging.gerLogger() 后跟 logger.info/debug/warning/error/ritic("some msg")

但是对于使用logger =logging.getLogger("someName")创建的子记录器,当我们编写logger.info/debug/warning/error/ritic("some msg"),并且没有配置处理程序,当 propagate = true 时,子记录器是否有默认处理程序?或者它是否回退到祖先的处理程序,然后返回到根记录器的处理程序,如果祖先/根记录器没有处理程序,则返回到根记录器 lastResort 处理程序?

如果传播设置为 false,那么是否会有一些默认处理程序分配给子记录器?

最佳答案

简短回答:logging.lastResort

长答案:

根据doc ,如果没有提供日志配置,则:

The event is output using a ‘handler of last resort’, stored in logging.lastResort. This internal handler is not associated with any logger, and acts like a StreamHandler which writes the event description message to the current value of sys.stderr (therefore respecting any redirections which may be in effect). No formatting is done on the message - just the bare event description message is printed. The handler’s level is set to WARNING, so all events at this and greater severities will be output.

同时根据logging source code :

class Logger(Filterer):
# ...
def callHandlers(self, record):
# ...
# found is the number of handlers
if (found == 0):
if lastResort:
if record.levelno >= lastResort.level:
lastResort.handle(record)

因此,如果没有处理程序,它将调用 lastResort,无论它是根记录器还是子记录器。 lastResort 实际上是 _StderrHandler(WARNING) ,这是:

class _StderrHandler(StreamHandler):
"""
This class is like a StreamHandler using sys.stderr, but always uses
whatever sys.stderr is currently set to rather than the value of
sys.stderr at handler construction time.
"""
def __init__(self, level=NOTSET):
"""
Initialize the handler.
"""
Handler.__init__(self, level)

@property
def stream(self):
return sys.stderr

所以你可以看到它实际上是一个具有警告级别的StreamHandler

关于python - 子记录器的默认处理程序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560505/

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