为模块创建新记录器是否仍使用根记录器名称?
Does creating new logger for module still use the root logger name?
我正在看这篇文章:https://docs.python.org/3.7/howto/logging.html#logging-basic-tutorial
段落:
The root of the hierarchy of loggers is called the root logger. That’s
the logger used by the functions debug(), info(), warning(), error()
and critical(), which just call the same-named method of the root
logger. The functions and the methods have the same signatures. The
root logger’s name is printed as ‘root’ in the logged output.
我很困惑,因为一旦你使用 logger = logging.getLogger(__name__)
创建了一个记录器并使用了 logger.info/debug/etc,模块的名称应该被打印出来,而不是 root。为什么该段落说:“...它只是调用根记录器的同名方法”
措辞很重要。
[...] That’s the logger used by the functions debug(), info(), warning(), error() and critical(), which just call the same-named method of the root logger. [...]
该段引用了模块级实用函数debug()
, info()
, warning()
, error()
and critical()
。事实上,所有这些都在根记录器上运行,例如:
def debug(msg, *args, **kwargs):
"""
Log a message with severity 'DEBUG' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
if len(root.handlers) == 0:
basicConfig()
root.debug(msg, *args, **kwargs)
这些模块级函数可以在 documentation.
中找到
当然,当通过 logging.getLogger(__name__)
创建您自己的记录器并在该实例上调用相应的方法时,您操作的是您的记录器,而不是根(主要是,仍然存在传播,除非明确禁用)。
编辑:
我刚刚意识到这个问题,以及你之前的问题, 已经回答了你稍早的问题。我强烈建议您仔细阅读。
我正在看这篇文章:https://docs.python.org/3.7/howto/logging.html#logging-basic-tutorial
段落:
The root of the hierarchy of loggers is called the root logger. That’s the logger used by the functions debug(), info(), warning(), error() and critical(), which just call the same-named method of the root logger. The functions and the methods have the same signatures. The root logger’s name is printed as ‘root’ in the logged output.
我很困惑,因为一旦你使用 logger = logging.getLogger(__name__)
创建了一个记录器并使用了 logger.info/debug/etc,模块的名称应该被打印出来,而不是 root。为什么该段落说:“...它只是调用根记录器的同名方法”
措辞很重要。
[...] That’s the logger used by the functions debug(), info(), warning(), error() and critical(), which just call the same-named method of the root logger. [...]
该段引用了模块级实用函数debug()
, info()
, warning()
, error()
and critical()
。事实上,所有这些都在根记录器上运行,例如:
def debug(msg, *args, **kwargs):
"""
Log a message with severity 'DEBUG' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
if len(root.handlers) == 0:
basicConfig()
root.debug(msg, *args, **kwargs)
这些模块级函数可以在 documentation.
中找到当然,当通过 logging.getLogger(__name__)
创建您自己的记录器并在该实例上调用相应的方法时,您操作的是您的记录器,而不是根(主要是,仍然存在传播,除非明确禁用)。
编辑:
我刚刚意识到这个问题,以及你之前的问题,