日志记录层次结构与根记录器?
在我的代码内部的某个地方,我有这样的东西:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 calllogging.getLogger('debug0.x')
creates a loggerx
which is a child ofdebug0
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 thebasicConfig()
function, so your message will end up there.如果您查看代码或文档:
logging.basicConfig 根本不使用名称参数。它初始化根记录器。而 getLogger 接受“名称”参数
If you check out the code or the doc:
logging.basicConfig does not use name argument at all. It initializes the root logger. While getLogger takes a "name" argument