Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - Logging hierarchy vs. root logger?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...