使用cgitb来简化异常调试
如果平时开发喜欢基于log的方式来调试,那么可能经常去做这样的事情,在log里面发现异常之后,因为信息不足,那么会再去额外加一些 debug log来把相关变量的值输出。调试完毕之后再把这些debug log去掉。其实没必要这么麻烦,Python库中提供了cgitb模块来帮助做这些事情,它能够输出异常上下文所有相关变量的信息,不必每次自己再去手 动加debug log。
cgitb的使用简单的不能想象:
def func(a, b):
return a / b if __name__ == '__main__': import cgitb cgitb.enable(format='text') import sys import traceback func(1, 0)
运行之后就会得到详细的数据:
A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred. /Users/samchi/Documents/workspace/tracebacktest/teststacktrace.py in <module>() 4 import cgitb 5 cgitb.enable(format='text') 6 import sys 7 import traceback 8 func(1, 0) func = <function func> /Users/samchi/Documents/workspace/tracebacktest/teststacktrace.py in func(a=1, b=0) 2 return a / b 3 if __name__ == '__main__': 4 import cgitb 5 cgitb.enable(format='text') 6 import sys a = 1 b = 0
完全不必再去log.debug("a=%d" % a)了,个人感觉cgitb在线上环境不适合使用,适合在开发的过程中进行调试,非常的方便。
也许你会问,cgitb为什么会这么屌?能获取这么详细的出错信息?其实它的工作原理同它的使用方式一样的简单,它只是覆盖了默认的sys.excepthook函数,sys.excepthook是一个默认的全局异常拦截器,可以尝试去自行对它修改:
def func(a, b):
return a / b def my_exception_handler(exc_type, exc_value, exc_tb): print "i caught the exception:", exc_type while exc_tb: print "the line no:", exc_tb.tb_lineno print "the frame locals:", exc_tb.tb_frame.f_locals exc_tb = exc_tb.tb_next if __name__ == '__main__': import sys sys.excepthook = my_exception_handler import traceback func(1, 0)
输出结果:
i caught the exception: <type 'exceptions.ZeroDivisionError'>
the line no: 14
the frame locals: {'my_exception_handler': <function my_exception_handler at 0x100e04aa0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': './teststacktrace.py', 'traceback': <module 'traceback' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'func': <function func at 0x100e04320>, '__name__': '__main__', '__doc__': None} the line no: 2 the frame locals: {'a': 1, 'b': 0}
看到没有?没有什么神奇的东西,只是从stack frame对象中获取的相关变量的值。frame对象中还有很多神奇的属性,就不一一探索了。