简体   繁体   English

为什么我不能使用logging.info记录异常?

[英]Why can't I log exceptions using logging.info?

For example, this bit of code below works as expected: 例如,下面的这段代码按预期工作:

import logging
import sys

def infologAllUncaught(exc_type, exc_value, exc_traceback):
    logging.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = infologAllUncaught

raise Exception("WOAH THERE!")

returning: 返回:

ERROR:root:Uncaught exception
Traceback (most recent call last):
  File "C:\dev\proj\cedu_ui\product\trunk\test\tools\genie\genie\logging_test2.py", line 9, in <module>
    raise Exception("WOAH THERE!")
Exception: WOAH THERE!

However, this bit of code returns nothing. 但是,这段代码不返回任何内容。

import logging
import sys

def infologAllUncaught(exc_type, exc_value, exc_traceback):
    logging.info("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = infologAllUncaught

raise Exception("WOAH THERE!")

It seems like all the logging methods except logging.exception have the same usage in the API, so I'm not sure why these are yielding different results. 看来除了logging.exception以外的所有日志记录方法在API中的用法都相同,因此我不确定为什么它们会产生不同的结果。

You probably need to configure your logger to log at INFO level, the default is only WARNING. 您可能需要将记录器配置为以INFO级别登录,默认值仅为WARNING。

Ref: docs.python.org/2/howto/logging.html#when-to-use-logging "The default level is WARNING ..." 参考:docs.python.org/2/howto/logging.html#when-to-use-logging“默认级别为警告...”

You might want to look at using something like this https://docs.python.org/2/howto/logging.html#logging-to-a-file to do the initial logger configuration. 您可能希望使用https://docs.python.org/2/howto/logging.html#logging-to-a-file这样的文件进行初始记录器配置。

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

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