python traceback使用

1、traceback.print_exc()

print_exc():是对异常栈输出


2、traceback.format_exc()

format_exc():是把异常栈以字符串的形式返回,print(traceback.format_exc()) 就相当于traceback.print_exc()


3、traceback.print_exception()

print_exception():traceback.print_exc()实现方式就是traceback.print_exception(sys.exc_info()),通过sys.exc_info()查看异常日志信息。

import sys
import transbacck
exc_type, exc_value, exc_tb = sys.exc_info() log.logger.error(traceback.format_exception(exc_type, exc_value, exc_tb, limit=20)) # 可设置打印日志的层数

日志级别分类:

DEBUG Level指出细粒度信息事件对调试应用程序是非常有帮助的。

INFO level表明 消息在粗粒度级别上突出强调应用程序的运行过程。

WARN level表明会出现潜在错误的情形。

ERROR level指出虽然发生错误事件,但仍然不影响系统的继续运行。

FATAL level指出每个严重的错误事件将会导致应用程序的退出。 

原文地址:https://www.cnblogs.com/hello-python2020/p/13858798.html