如何用Python将异常错误堆栈信息记录到日志文件中?
- 内容介绍
- 文章标签
- 相关推荐
本文共计886个文字,预计阅读时间需要4分钟。
将伪原创内容进行简化改写,不使用错别字,不超过100字:
python设置日志记录异常信息到log.txt文件import logginglogging.basicConfig(filename='log.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')try: raise 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标准日志管理维护工具。
本文共计886个文字,预计阅读时间需要4分钟。
将伪原创内容进行简化改写,不使用错别字,不超过100字:
python设置日志记录异常信息到log.txt文件import logginglogging.basicConfig(filename='log.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')try: raise 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标准日志管理维护工具。

