您的位置:首页 > 编程语言 > Python开发

python错误处理记录完整的异常堆栈信息

2017-10-24 17:38 573 查看
import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

logging.debug('This message should go to the log file')

try:
run_my_stuff()
except:
logging.exception('Got exception on main handler')
raise
这样logging.exception方法会自动记录异常信息,如下样例:

DEBUG:root:This message should go to the log file
ERROR:root:Got exception on main handler
Traceback (most recent call last):
File "/tmp/teste.py", line 9, in <module>
run_my_stuff()
NameError: name 'run_my_stuff' is not defined
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python django
相关文章推荐