Python 日志层次结构与根记录器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4150148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:28:05  来源:igfitidea点击:

Logging hierarchy vs. root logger?

pythonlogging

提问by reckoner

Somewhere in the bowels of my code I have something like:

在我的代码内部的某个地方,我有类似的东西:

logger = logging.getLogger('debug0.x')

The way I understand it, this should onlyrespond when I have previously done something like:

按照我的理解,这应该在我以前做过类似的事情时才会响应:

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG, name='debug0')

note that namehas been defined as debug0. However, I have discovered that if do

请注意,name已定义为debug0。但是,我发现如果这样做

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG)

without the namekeyword, then the debug0.xlogger defined above reacts, and writes to the log file. I was thinking it would only react in the first case, when the logger had been named.

如果没有name关键字,上面定义的debug0.x记录器会做出反应,并写入日志文件。我认为它只会在第一种情况下做出反应,当记录器被命名时。

I'm confused.

我糊涂了。

采纳答案by Sven Marnach

The Python loggingmodule organizes loggers in a hierarchy. All loggers are descendants of the root logger. Each logger passes log messages on to its parent.

Pythonlogging模块按层次结构组织记录器。所有记录器都是根记录器的后代。每个记录器将日志消息传递给其父记录器。

New loggers are created with the getLogger()function. The function call logging.getLogger('debug0.x')creates a logger xwhich is a child of debug0which itself is a child of the root logger. When logging to this logger, it will pass on the message to its parent, and its parent will pass the message to the root logger. You configured the root logger to log to a file by the basicConfig()function, so your message will end up there.

使用该getLogger()函数创建新的记录器。函数调用logging.getLogger('debug0.x')创建一个记录器x,它是其子记录器,debug0它本身是根记录器的子记录器。当登录到这个记录器时,它会将消息传递给它的父记录器,它的父记录器会将消息传递给根记录器。您将根记录器配置为通过basicConfig()函数记录到文件,因此您的消息将在那里结束。

回答by pyfunc

If you check out the code or the doc:

如果您查看代码或文档:

>>> print logging.basicConfig.__doc__

    Do basic configuration for the logging system.

    This function does nothing if the root logger already has handlers
    configured. ...............
    A number of optional keyword arguments may be specified, which can alter
    the default behaviour.

    filename  Specifies that a FileHandler be created, using the specified
              filename, rather than a StreamHandler.
    filemode  Specifies the mode to open the file, if filename is specified
              (if filemode is unspecified, it defaults to 'a').
    format    Use the specified format string for the handler.
    datefmt   Use the specified date/time format.
    level     Set the root logger level to the specified level.
    stream    Use the specified stream to initialize the StreamHandler. Note
              that this argument is incompatible with 'filename' - if both
              are present, 'stream' is ignored.

logging.basicConfig does not use name argument at all. It initializes the root logger. While getLogger takes a "name" argument

logging.basicConfig 根本不使用 name 参数。它初始化根记录器。虽然 getLogger 采用“名称”参数

>>> print logging.getLogger.__doc__

    Return a logger with the specified name, creating it if necessary.

    If no name is specified, return the root logger.