在Python 3中记录异常

安德鲁

我有以下内容:

### 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:无”输出?

威姆

stdliblogging尝试附加异常信息,但是您没有任何异常信息,因为您不是从except:内登录NoneType: None最终来自的结果sys.exc_info()调用,记录不会刻意去检查他们是否是有效还是无效。

me脚的解决方法:

logger.exception('this is an exception', exc_info=False)

la脚少一点:

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章