我如何使用python日志记录框架在警告或信息级别通过回溯记录异常? [英] How do I log an exception at warning- or info-level with traceback using the python logging framework?

查看:83
本文介绍了我如何使用python日志记录框架在警告或信息级别通过回溯记录异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用类似这样的内容:

try:
   # Something...
except Exception as excep:
   logger = logging.getLogger("component")
   logger.warning("something raised an exception: " + excep)
   logger.info("something raised an exception: " + excep)

在特殊情况下,我宁愿不将其放在错误级别原因上,这也不是错误.

I would rather not have it on the error-level cause in my special case it is not an error.

推荐答案

来自

kwargs中有三个要检查的关键字参数:exc_infostack_infoextra.

There are three keyword arguments in kwargs which are inspected: exc_info, stack_info, and extra.

如果exc_info的评估结果不为假,则会导致将异常信息添加到日志消息中.如果是异常元组(格式为 sys.exc_info() ),或者提供了一个异常实例;否则,将调用 sys.exc_info() 来获取异常信息.

If exc_info does not evaluate as false, it causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) or an exception instance is provided, it is used; otherwise, sys.exc_info() is called to get the exception information.

这样做:

logger.warning("something raised an exception:", exc_info=True)

这篇关于我如何使用python日志记录框架在警告或信息级别通过回溯记录异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆