[英]Logging hierarchy vs. root logger?
在我的代码的某处,我有类似以下内容:
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记录器将做出反应,并写入日志文件。 我以为只有在命名记录器时,它才会在第一种情况下做出反应。
我糊涂了。
Python logging
模块按层次结构组织记录器。 所有记录器都是根记录器的后代。 每个记录器将日志消息传递到其父级。
使用getLogger()
函数创建新的记录器。 函数调用logging.getLogger('debug0.x')
创建一个记录器x
,它是debug0
的子代,而debug0
本身是根记录器的子代。 登录到该记录器时,它将消息传递给它的父记录器,其父记录将消息传递给根记录器。 您已配置了root记录器以通过basicConfig()
函数登录到文件,因此您的消息将在那里结束。
如果您签出代码或文档:
>>> 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根本不使用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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.