丰富的回溯记录器
rich-traceback的Python项目详细描述
python的信息性回溯日志记录
显示方法参数的信息堆栈跟踪。 支持控制台系统日志的简单独立记录器。
用法:
- 如果您已经在使用日志模块,请在模块中导入rich_traceback.enable。然后logger.exception()将报告格式良好的异常。
导入富跟踪。启用
- 否则,使用richtracebackformatter类创建一个新的记录器,或者以rich traceback.log为例:
(另存为test.py)
from rich_traceback.formatter import RichTracebackFormatter import logging logger = logging.getLogger('root') console_log = logging.StreamHandler() console_log.setFormatter(RichTracebackFormatter()) logger.addHandler(console_log) def foo(x=3): if 1.0/x: foo(x-1) try: foo() except: logger.exception("error running foo") print "compare with standard Python traceback" foo() # for standard traceback
$ python test.py ERROR root test.<module>:15 error running foo <type 'exceptions.ZeroDivisionError'>: ZeroDivisionError('float division by zero',) ([5] frames following) [4] test.py, foo(x=0) at line 9: if 1.0/x: [3] test.py, foo(x=1) at line 10: foo(x-1) [2] test.py, foo(x=2) at line 10: foo(x-1) [1] test.py, foo(x=3) at line 10: foo(x-1) [0] test.py, <module>() at line 13: foo() compare with standard Python traceback Traceback (most recent call last): File "test.py", line 18, in <module> foo() File "test.py", line 10, in foo foo(x-1) File "test.py", line 10, in foo foo(x-1) File "test.py", line 10, in foo foo(x-1) File "test.py", line 9, in foo if 1.0/x: ZeroDivisionError: float division by zero
有关全局异常陷阱,请参见enable.py