[英]How do I log an exception at warning- or info-level with traceback using the python logging framework?
Using something like this: 使用这样的东西:
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. 我宁愿没有错误级别的原因在我的特殊情况下它不是一个错误。
From the logging documentation : 从日志记录文档 :
There are three keyword arguments in
kwargs
which are inspected:exc_info
,stack_info
, andextra
.kwargs
有三个关键字参数被检查:exc_info
,stack_info
和extra
。If
exc_info
does not evaluate as false, it causes exception information to be added to the logging message. 如果exc_info
未评估为false,则会将异常信息添加到日志消息中。 If an exception tuple (in the format returned bysys.exc_info()
) or an exception instance is provided, it is used; 如果提供了异常元组(以sys.exc_info()
返回的格式)或异常实例,则使用它; otherwise,sys.exc_info()
is called to get the exception information. 否则,调用sys.exc_info()
以获取异常信息。
So do: 所以:
logger.warning("something raised an exception:", exc_info=True)
这是一个有效的(python 2.6.5)。
logger.critical("caught exception, traceback =", exc_info=True)
You can try this: 你可以试试这个:
from logging import getLogger
logger = getLogger('warning')
try:
# Somethings that is wrong.
except Exception as exp:
logger.warning("something raised an exception: " , exc_info=True)
logger.warning("something raised an exception: {}".format(exp)) # another way
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.