我如何使用python日志记录框架在警告或信息级别通过回溯记录异常? [英] How do I log an exception at warning- or info-level with traceback using the python logging framework?
问题描述
使用类似这样的内容:
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.
推荐答案
来自
在 There are three keyword arguments in 如果 If 这样做: 这篇关于我如何使用python日志记录框架在警告或信息级别通过回溯记录异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!kwargs
中有三个要检查的关键字参数:exc_info
,stack_info
和extra
.
kwargs
which are inspected: exc_info
, stack_info
, and extra
.exc_info
的评估结果不为假,则会导致将异常信息添加到日志消息中.如果是异常元组(格式为 sys.exc_info()
),或者提供了一个异常实例;否则,将调用 sys.exc_info()
来获取异常信息. 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)