环境 - win7机器上的Python 2.7.2。 我的技能水平 - 菜鸟
我正在使用以下内容来捕获和打印异常堆栈跟踪 -
def logerr(stmt, e):
try:
##do something
except:
print '##EXCEPTION in logging: '
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout)
输出是 -
##EXCEPTION in logging:
Traceback (most recent call last):
File "C:\Users\amurty\Desktop\dev\eclipse\workspace\hhs\FeedSearch\src\main\main.py", line 18, in main
log()
TypeError: log() takes exactly 1 argument (0 given)
我想缩进堆栈跟踪。所以输出应该是这样的 -
##EXCEPTION in logging:
Traceback (most recent call last):
File "C:\Users\amurty\Desktop\dev\eclipse\workspace\hhs\FeedSearch\src\main\main.py", line 18, in main
log()
TypeError: log() takes exactly 1 argument (0 given)
我怎样才能做到这一点。 pprint或textwrap模块会在这里提供帮助吗?
答案 0 :(得分:3)
试试这个:
import traceback
def logerr(stmt, e):
try:
##do something
except:
print '##EXCEPTION in logging: '
for line in traceback.format_exception().splitlines():
print ' ' + line