从文本中提取 Python Tracebacks
项目描述
用于从文本中提取 Python 回溯的模块和命令行工具。
从一堆文件中提取回溯
$ tbgrep file1 file2 file3
在管道中寻找回溯
$ tail -f logfile | tbgrep
显示按出现次数排序的所有唯一回溯
$ tbgrep --stats logfile
[...]
== 99 occurences ==================================================
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/bodhi/admin.py", line 209, in _masher_request
req_params=kwargs)
File "/usr/lib/python2.4/site-packages/fedora/client/proxyclient.py", line 285, in send_request
raise AuthError(_('Unable to log into server. Invalid'
AuthError: Unable to log into server. Invalid authentication tokens. Send new username and password
==================================================================
733 unique tracebacks extracted
使用 Python API
实例化后,将每一行传递给process方法,该方法将返回 None 或回溯字符串。
from tbgrep import TracebackGrep
extractor = TracebackGrep()
for line in file('logfile'):
tb = extractor.process(line)
if tb:
print(tb)
tbgrep 不显示文件中找到的每个回溯,还支持生成有关文件中所有回溯的统计信息。
extractor = TracebackGrep(stats=True)
for line in file('logfile'):
extractor.process(line)
extractor.print_stats()
还有一些功能可以让您以更方便的方式搜索回溯。
from tbgrep import (
tracebacks_from_lines, tracebacks_from_file,
last_traceback_from_file)
for tb in tracebacks_from_file(file('logfile')):
print(tb)
for tb in tracebacks_from_file(file('logfile'), reverse=True):
print(tb)
print(last_traceback_from_file(file('logfile')))
支持的输入格式
tbgrep 可以从各种格式的日志中提取回溯。例如,CherryPy 在具有其他详细信息(如模块、时间戳等)的行上开始回溯,但跟踪的其余部分从行首开始。另一方面,Apache 日志将在回溯的每一行添加此信息的前缀。tbgrep 旨在处理这些情况