且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过对象图了解Python日志记录

更新时间:2023-12-05 14:30:22

所有三个语句都是正确的,但是第三个语句有点奇怪.

All three statements are correct, the third is a little weird though.

A:记录器2和记录器3共享同一个文件处理程序,因此它们的输出将混合(按时间戳记).

A: Loggers 2 and 3 share the same file handler, hence their output will mix (by timestamp).

请注意,记录器2和3可以具有其他处理程序,它们可以共享更多的处理程序,也可以拥有自己的处理程序.每个记录器可以有多个处理程序.

Note that loggers 2 and 3 could have additional handlers, they could share more or have their own. Each logger can have multiple handlers.

B:记录器1使用其他文件处理程序,因此它将具有自己的输出文件.

B: Logger 1 uses a different file handler, hence it will have its own output file.

一个记录器,一个处理程序,一个输出文件.容易理解.

One logger, one handler, one output file. Easy to understand.

C:此对象图可能涉及五个不同的日志级别.

C: There are potentially five different log levels involved for this object diagram.

是的,但是与每个记录器关联的三个级别所做的事情与与处理程序关联的级别有所不同.每个记录器上的级别告诉记录器完全忽略该级别以下的所有消息.因此,例如,如果您有一个处理DEBUG消息的处理程序,但该处理程序位于设置了INFO级别的记录器上,则该处理程序将永远不会从该特定记录器发送DEBUG消息.因此,记录器将获取记录器级别或更高级别的所有消息,并将其转发给它拥有的每个处理程序.在Python日志记录高级教程中href ="https://docs.python.org/3/howto/logging.html#logging-advanced-tutorial" rel ="nofollow noreferrer">查看以下流程图:

True, but the three levels associated with each logger do something different than the levels associated with the handlers. The levels on each logger tell the logger to entirely ignore all messages below that level. So for instance, if you have a handler that handles DEBUG messages, but that is on a logger with INFO set as the level, the handler will never send DEBUG messages from that specific logger. So, the logger will take all messages at or above the logger level, and forward them off to each handler it has. Check the following flow diagram from the Python logging advanced tutorial:

该图很容易说明,因此我不会对其进行过多地说明.但这有望消除您的所有疑问.

The diagram is pretty self explanatory, so I won't rehash it too much. But this should hopefully clear all your doubts.