简体   繁体   English

Python日志记录:为使用过的模块的所有记录器设置处理程序

[英]Python logging: Set handlers for all loggers of used modules

I have my main script that interprets cli commands with argparse and then starts the application by calling the according stuff form an other module (made by myself). 我有我的主要脚本,该脚本使用argparse解释cli命令,然后通过从另一个模块(由我自己制作)中调用相应的东西来启动应用程序。

My question now is how I can attach a handler to the logger from that module. 我现在的问题是如何从该模块将处理程序附加到记录器。 The logger is retrieved with 记录器使用

logger = logging.getLogger(__name__)

I hence put following in my main script: 因此,我在主脚本中添加了以下内容:

consoleHandler = logging.StreamHandler()
logger = logging.getLogger('MyModule')
logger.addHandler(consoleHandler)

However there is 0 logging output from 'MyModule'. 但是,“ MyModule”的日志记录输出为0。 Log levels are correct, eg there should be an output. 日志级别正确,例如应该有一个输出。

In MyModule I have the following: MyModule我具有以下内容:

logging.getLogger(__name__).addHandler(logging.NullHandler())

However removing that makes no difference. 但是,删除该文件没有任何区别。

So how can I correctly attach a handler to the logger of MyModule ? 那么,如何正确将处理程序附加到MyModule的记录器?

The best example of adding handler to logger is the one from Docs , See below. handler添加到记录器的最佳示例是Docs中的示例,请参见下文。

Did you added consoleHandler with setLevel() and the Formatter() ? 是否添加consoleHandler带有setLevel()Formatter() consoleHandler

As for this line: logging.getLogger(__name__).addHandler(logging.NullHandler()) 对于这一行: logging.getLogger(__name__).addHandler(logging.NullHandler())

Why are you using it this way? 为什么用这种方式? I would recommend to follow the next code for adding an handler the right way. 我建议遵循下面的代码以正确的方式添加处理程序。

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)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

The reason you're getting no output is most likely that you haven't set a level on the logger, so it will default to WARNING . 没有输出的原因很可能是您未在记录器上设置级别,因此它将默认为WARNING Try doing logger.setLevel(logging.DEBUG) and see if that causes output to be produced. 尝试执行logger.setLevel(logging.DEBUG) ,看看是否导致产生输出。 I know you said that log levels are correct, but I can't see any other reason why no output would be produced. 我知道您说过日志级别是正确的,但是我看不到没有其他任何原因导致无法产生任何输出。 You can try posting a small self-contained script that demonstrates the problem, if you like. 如果愿意,您可以尝试发布一个小的独立脚本来演示问题。

NullHandler only needs to be added for library modules to cater for their use in an application that doesn't configure logging. 仅需要为库模块添加NullHandler即可满足它们在未配置日志记录的应用程序中的使用。 It is not needed in modules of an application that configures logging. 在配置日志记录的应用程序模块中不需要它。

In general, you can add a handler to the root logger and it will be used by loggers in all modules. 通常,您可以向根记录器添加一个处理程序,所有模块中的记录器都将使用该处理程序。

Update: The comment to Kobi K's answer indicates that a level has been set on the handler - this is not enough, as you need to set a level on the logger, whose level is checked first. 更新: Kobi K答案的注释表明已在处理程序上设置了一个级别-这还不够,因为您需要在记录器上设置一个级别,首先要检查其级别。 Only if the event is allowed through by the logger's level will the handlers (and their levels) come into play. 仅当记录器级别允许通过事件时,处理程序(及其级别)才会发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM