记录层次结构与root记录器? [英] Logging hierarchy vs. root logger?

查看:77
本文介绍了记录层次结构与root记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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')

请注意,名称已定义为 debug0 .但是,我发现如果可以

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

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

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

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.

我很困惑.

推荐答案

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

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.

使用getLogger()功能创建新的记录器.函数调用logging.getLogger('debug0.x')创建一个记录器x,它是debug0的子代,而它又是根记录器的子代.登录到该记录器时,它将消息传递给它的父记录器,其父记录将消息传递给根记录器.您已通过basicConfig()功能将根记录程序配置为登录到文件,因此您的消息将在那里结束.

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.

这篇关于记录层次结构与root记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆