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

发布于 2024-10-01 20:34:46 字数 537 浏览 10 评论 0原文

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

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

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

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

注意名称已被定义作为debug0。但是,我发现如果

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

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

我很困惑。

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

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

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

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

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

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

without the name keyword, then the debug0.x logger 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.

I'm confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2024-10-08 20:34:46

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

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

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

New loggers are created with the getLogger() function. The function call logging.getLogger('debug0.x') creates a logger x which is a child of debug0 which 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.

烟酉 2024-10-08 20:34:46

如果您查看代码或文档:

>>> 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 根本不使用名称参数。它初始化根记录器。而 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.

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

>>> print logging.getLogger.__doc__

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

    If no name is specified, return the root logger.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文