logging setLevel, how it works

后端 未结 3 683
遥遥无期
遥遥无期 2020-12-14 05:57

In the logging howto documentation there is this example:

import logging

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


        
3条回答
  •  抹茶落季
    2020-12-14 06:40

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

提交回复
热议问题