记录 setLevel,它是如何工作的

发布于 2024-11-19 02:39:29 字数 1082 浏览 3 评论 0原文

日志记录指南文档中有一个示例:

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

为什么我应该将级别设置为logging.DEBUG 两次,分别用于 LoggerStreamHandler

我了解 ch.setLevel(logging.DEBUG) 将为流处理程序设置调试级别。但是设置logger的level有什么效果呢?这个水平体现在哪里?

例如,如果将级别更改为 LoggerStreamHandlerINFO,我会得到相同的控制台输出。

也就是说:

...........
logger.setLevel(logging.INFO)
............
ch.setLevel(logging.DEBUG)

在控制台中给出的输出与

...........
logger.setLevel(logging.DEBUG)
............
ch.setLevel(logging.INFO)

In the logging howto documentation there is this example:

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

Why I should set the level to logging.DEBUG twice, for Logger, and for the StreamHandler?

I understand ch.setLevel(logging.DEBUG) will set the debug level for the stream handler. But what the effect is of setting the level to logger? Where this level is reflected?

I get the same console output if I change the level to, for example, INFO either to the Logger or to the StreamHandler.

That is:

...........
logger.setLevel(logging.INFO)
............
ch.setLevel(logging.DEBUG)

gives the same output in console than

...........
logger.setLevel(logging.DEBUG)
............
ch.setLevel(logging.INFO)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-11-26 02:39:29

它用于微调(您可以有多个处理程序,每个处理程序可以设置不同的级别)-您可以安全地不在处理程序上设置级别,这将导致它处理所有消息(也称为 NOTSET 级别),并保留级别过滤到记录器。

Logger 也是第一个根据级别过滤消息的 - 如果您将记录器设置为 INFO,并将所有处理程序设置为 DEBUG,您仍然不会在处理程序上收到 DEBUG 消息 - 它们将被记录器本身拒绝。如果您将记录器设置为 DEBUG,但将所有处理程序设置为 INFO,您也不会收到任何 DEBUG 消息 - 因为虽然记录器说“好的,处理这个”,但处理程序拒绝它(DEBUG < INFO)。

It's there for fine-tuning (you can have multiple handlers, and each could have different levels set) — you can safely not set level on the handler, which will cause it to process all messages (a.k.a. NOTSET level), and leave level filtering to the logger.

Logger is also the first to filter the message based on a level — if you set the logger to INFO, and all handlers to DEBUG, you still won't receive DEBUG messages on handlers — they'll be rejected by the logger itself. If you set logger to DEBUG, but all handlers to INFO, you won't receive any DEBUG messages either — because while the logger says "ok, process this", the handlers reject it (DEBUG < INFO).

握住我的手 2024-11-26 02:39:29

为什么我应该将记录器和流处理程序的级别设置为logging.DEBUG两次。我了解 ch.setLevel(logging.DEBUG) 将为流处理程序设置调试级别。但是设置level为logger有什么效果呢?这个级别反映在哪里?。

文档中对此进行了说明:

“setLevel() 方法,就像在记录器对象中一样,指定将分派到适当目的地的最低严重性。为什么会有两个 setLevel() 方法?记录器中设置的级别决定了它将传递给其处理程序的消息的严重性。每个处理程序中设置的级别决定了处理程序将发送哪些消息。”

在处理程序下进行检查:http://docs.python.org/2.7/howto/logging.html #日志记录高级教程

Why I should set the level to logging.DEBUG twice, for logger, and for the streamhandler. I understand ch.setLevel(logging.DEBUG) will set the debug level for the stream handler. But what the effect is of setting the level to logger?. Where this level is reflected?.

This is indicated in the documentation:

"The setLevel() method, just as in logger objects, specifies the lowest severity that will be dispatched to the appropriate destination. Why are there two setLevel() methods? The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on."

Check under Handlers: http://docs.python.org/2.7/howto/logging.html#logging-advanced-tutorial

始终不够 2024-11-26 02:39:29

我认为考虑以下三点对于理解日志记录的工作原理很有用:

  • 您可以构建 Logger 对象的层次结构。他们每个人都会
    最初没有级别设置(级别 NOTSET)。的有效水平
    Logger 对象是层次结构中设置的第一个级别
    在到达根记录器的路上(如果没有级别,可能是 NOTSET)
    set)。

  • Logger 的有效级别,仅用于确定是否直接向该 logger 发送消息来启动操作。

  • 该操作首先将消息传递给 Logger 的处理程序,
    其次(取决于 propagate 标志的值),将其传递给祖先链顶部的每个处理程序,而不考虑每个记录器的实际级别 该操作

要回答您的问题,您不需要在该示例中设置两次。仅在 Logger 中将其设置为 DEBUG 就足以使日志消息能够直接发送到 Logger 实例以到达控制台(因为新 StreamHandler 中的默认级别默认为 NOTSET,因此它将让一切通过)。

I think is useful to consider these main three points to understand how logging works:

  • You can build a hierarchy of Logger objects. Each of them will
    initially have no level set (level NOTSET). The effective level of a
    Logger object is the first level that has been set in the hierarchy
    on the way up to the root logger (possibly NOTSET, if no level has been
    set).

  • The effective level of a Logger, is only used to determine whether to start action with message directly emitted to that logger.

  • That action is, first, passing the message to that Logger's handlers,
    and second (depending on the value of the propagate flag), passing it to each of the handlers of the chain of ancestors to the top, without taking into consideration the actual levels of each of the loggers in the chain.

To answer your question, you don't need to set it twice in that example. Setting it to DEBUG only in the Logger will be enough to enable log messages sent directly to your Logger instance to make their way to the console (since the default level in a new StreamHandler is NOTSET by default, so it will let everything pass).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文