[英]Determining if root logger is set to DEBUG level in Python?
If I set the logging module to DEBUG with a command line parameter like this: 如果我使用命令行参数将日志记录模块设置为DEBUG:
if (opt["log"] == "debug"):
logging.basicConfig(level=logging.DEBUG)
How can I later tell if the logger was set to DEBUG? 我怎么能告诉记录器是否设置为DEBUG? I'm writing a decorator that will time a function if True flag is passed to it, and if no flag is given, it defaults to printing timing information when the root logger is set to DEBUG. 我正在编写一个装饰器,如果传递了True标志,它将为一个函数计时,如果没有给出标志,它将默认为在根记录器设置为DEBUG时打印定时信息。
logging.getLogger().getEffectiveLevel()
logging.getLogger()
without arguments gets the root level logger. 不带参数的logging.getLogger()
获取根级别记录器。
http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel
Actually, there's one better: use the code logging.getLogger().isEnabledFor(logging.DEBUG)
. 实际上,还有一个更好: 使用代码logging.getLogger().isEnabledFor(logging.DEBUG)
。 I found it while trying to understand what to do with the result of getEffectiveLevel()
. 我在试图理解如何处理getEffectiveLevel()
的结果时发现了它。
Below is the code that the logging module itself uses. 以下是日志记录模块本身使用的代码。
def getEffectiveLevel(self):
"""
Get the effective level for this logger.
Loop through this logger and its parents in the blogger hierarchy,
looking for a non-zero logging level. Return the first one found.
"""
logger = self
while logger:
if logger.level:
return logger.level
logger = logger.parent
return NOTSET
def isEnabledFor(self, level):
"""
Is this logger enabled for level ‘level’?
"""
if self.manager.disable >= level:
return 0
return level >= self.getEffectiveLevel()
只是
logging.getLogger().level == logging.DEBUG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.