我有以下几点:
### running with Python 3
import logging
logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')
logger.exception('this is an exception')
输出是:
this is an exception
NoneType: None
我需要做什么才能摆脱“NoneType:None”输出?
最佳答案
标准库 logging
is trying to append the exception info ,但您没有任何异常信息,因为您不是从 except:
内登录的堵塞。 NoneType: None
最终来自sys.exc_info()
的结果调用,日志记录不会检查它们是否有效。
跛脚解决方法:
logger.exception('this is an exception', exc_info=False)
少一点蹩脚:
logger.error('this is an exception...haha not really')
最好的事物:
try:
# risky code
except Exception:
logger.exception('this is an exception')
如果您有一个较早捕获的异常实例,并且您想记录它,并且您不在一个 except 块中,这将做正确的事情:
logger.exception('this is an exception', exc_info=myerror)
关于python-3.x - 在 Python 3 中记录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162817/