Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在忽略日志记录setLevel_Python_Logging - Fatal编程技术网

Python 正在忽略日志记录setLevel

Python 正在忽略日志记录setLevel,python,logging,Python,Logging,下面的代码是从文档中复制的。我应该能看到所有的信息日志。但我没有。即使我已将setLevel设置为INFO,我也只能看到警告和更高级别 为什么会这样foo.py: import logging logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.debug('debug message') logger.info('info message') logger.warn('warn message

下面的代码是从文档中复制的。我应该能看到所有的信息日志。但我没有。即使我已将setLevel设置为INFO,我也只能看到警告和更高级别

为什么会这样<代码>foo.py:

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
输出:

workingDirectory$ python foo.py
warn message
error message
critical message
信息和调试消息到哪里去了???

尝试在那里运行。请注意,我看到您提到了信息,但使用了调试。如前所述,它应该显示所有五条消息。将DEBUG换成INFO,您将看到四条消息

import logging

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
编辑:您是否已经在代码的其他地方设置了日志记录?无法使用提供的特定代码再现您注意到的确切行为。

替换该行

logger.setLevel(logging.DEBUG)


它应该像预期的那样工作。如果您没有使用任何处理程序配置日志记录(如在文章中-您只为日志记录程序配置了一个级别,但在任何地方都没有处理程序),您将得到一个内部处理程序,该处理程序设置为仅在
警告级别输出消息(没有其他格式)。

这在技术上也是一个“答案”,因为它可以“解决”这个问题。但我绝对不喜欢它。这不是直观的,我为此损失了2个多小时

之前:

import logging
logger = logging.getLogger('foo')
logger.setLevel(logging.INFO)
logger.info('You can not see me')
# Or you can just use the following one-liner in command line.
# $ python -c "import logging; logger = logging.getLogger('foo'); logger.setLevel(logging.INFO); logger.info('You can not see me')"
之后:

import logging

logging.debug('invisible magic')  # <-- magic

logger = logging.getLogger('foo')
logger.setLevel(logging.INFO)
logger.info('But now you can see me')
# Or you can just use the following one-liner in command line.
$ python -c "import logging; logging.debug('invisible magic'); logger = logging.getLogger('foo'); logger.setLevel(logging.INFO); logger.info('But now you see me')"
导入日志

logging.debug('invisibleMagic')#接受的答案在Win10、Python 3.7.2上对我不起作用

我的解决方案:

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

它对订单敏感。

正如一些用户所指出的,使用:

logging.basicConfig(level=logging.DEBUG, format='%(message)s')
正如在接受的answare中编写的一样,它不是一个傻瓜选项,因为它全局设置日志级别,所以它将记录来自每个记录器的调试消息

就我而言,仅为我的记录器设置日志级别的最佳解决方案是:

import logging

logger = logging.getLogger('MyLogger')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

这不是真正直观的解决方案,但如果您只想为“MyLogger”设置日志级别,而不是全局设置日志级别,这是必需的。

您必须将根日志记录器的基本配置设置为调试,然后您可以将单个日志记录器的级别设置为更严格的级别

这不是我所期望的。以下是我必须做的:

#!/usr/bin/env python3

import logging
# by default this is WARNING.  Leaving it as WARNING here overrides 
# whatever setLevel-ing you do later so it seems they are ignored.
logging.basicConfig(level=logging.DEBUG)

l = logging.getLogger(__name__)
l.setLevel(level=logging.INFO)
# if I hadn't called basicConfig with DEBUG level earlier, 
# info messages would STILL not be shown despite calling 
# setLevel above.  However now debug messages will not be shown 
# for l because setLevel set it to INFO

l.warning('A warning message will be displayed')
l.info('A friendly info message will be displayed')
l.debug('A friendly debug message will not be displayed')

如果希望在没有basicConfig的情况下工作,则必须首先设置登录到记录器的最低级别。由于记录器设置了最小阈值,因此具有较低阈值但属于同一记录器的处理程序将不会获得这些较低阈值消息,因为记录器首先会忽略这些消息。直观,但不明显

我们从这样做开始:

lgr = logging.getLogger(name)
lgr.setLevel(logging.DEBUG)
然后,按照您需要的不同级别设置处理程序,在我的示例中,我希望在标准输出上进行调试日志记录,并将信息日志记录到旋转文件中,因此我执行以下操作:

rot_hndlr = RotatingFileHandler('filename.log',
                                maxBytes=log_size,
                                backupCount=3)
    
rot_hndlr.setFormatter(formatter)
rot_hndlr.setLevel(logging.INFO)
lgr.addHandler(rot_hndlr)

stream_hndlr = logging.StreamHandler()
stream_hndlr.setFormatter(stream_formatter)
lgr.addHandler(stream_hndlr)
然后,为了测试,我执行以下操作:

lgr.debug("Hello")
lgr.info("There")
我的stdout(控制台)将如下所示:

Hello
There
There
我的
filename.log
文件如下所示:

Hello
There
There

您好@tabbek,谢谢您的回复。我将尝试使用basicConfig()来代替。今晚晚些时候我可以做。我没有任何其他代码。我有一个处理程序的另一个项目,它可以工作。然后我尝试开始一个新项目,但没有成功。所以我写了这个赤裸裸的剧本。以上代码是
foo.py
的唯一内容。我在想有什么干扰在发生什么。我会让你不断更新!basicConfig()执行一些基本设置,并创建一个新的日志处理程序。basicConfig([**kwargs])应该附加一个具有警告级别的处理程序。这里的“logger.setLevel(logging.DEBUG)”是否同时设置了记录器和处理程序级别?谢谢Vinay,您维护了
日志记录
??感谢您抽出时间回答一个新手问题!今晚晚些时候我将尝试此方法,并确认它是否有效。请稍候-这将设置全局/根日志记录设置,但如果我只想为
记录器设置级别,该怎么办?为什么
logger.setLevel(logging.DEBUG)
?应该是这样的。@Greg,因为没有配置处理程序,所以日志记录使用内部的“最后的处理程序”,其级别为
警告
:但是,它应该可以工作。用户显式设置级别。因此在getLogger()之后调用
logger.setlevel(logging.DEBUG)
是不够的。我还得和一个处理员搞混?这一点都不直观。这确实令人困惑,这让我问,我发现被接受的答案非常有用:-)@sam,谢谢你的提醒!是的,这使我现在睡得很好-令人愉快的回答。谢谢帮了我很多忙!非常感谢,我一直在找这个!这一个实际上根本不需要设置级别,但也意味着它与上面更清晰的答案相同。