错误堆栈信息处理

  

Python把Exception异常错误堆栈信息写入日志文件


假设需要把发生异常错误的信息写入到log.txt日志文件中去:

import traceback
import logging

logging.basicConfig(filename='log.txt', level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')

try:
  raise Exception('发生异常错误信息')
except:
  #方案一,自己定义一个文件,自己把错误堆栈信息写入文件。
  #errorFile = open('log.txt', 'a')
  #errorFile.write(traceback.format_exc())
  #errorFile.close()

  #方案二,使用Python标准日志管理维护工具。
  logging.debug(traceback.format_exc())

posted @ 2020-02-17 09:49  yang-leo  阅读(465)  评论(0)    收藏  举报