logging.exception在
except:处理程序/块中使用,可将当前异常与跟踪信息一起记录在日志中,并附带一条消息。
import loggingLOG_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现在查看日志文件
/tmp/logging_example.out:
DEBUG:root:This message should go to the log fileERROR:root:Got exception on main handlerTraceback (most recent call last): File "/tmp/teste.py", line 9, in <module> run_my_stuff()NameError: name 'run_my_stuff' is not defined



