Created
February 27, 2012 10:29
-
-
Save originell/1923003 to your computer and use it in GitHub Desktop.
Python Traceback Extractor from text/log files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Extract unique Python-Exceptions with their Traceback from a log/text file. | |
Usage:: | |
python extract_exceptions.py -f logfile.txt | |
Furthermore it supports excluding exceptions you don't want to have:: | |
python extract_exceptions.py -f logfile.txt -e ValueError,AttributeError | |
Would exclude any ``ValueError`` or ``AttributeError`` from the list. | |
""" | |
from optparse import OptionParser | |
parser = OptionParser() | |
parser.add_option('-f', '--file', dest='file', | |
help='The file to extract exceptions from', | |
default="", | |
metavar='FILE') | |
parser.add_option('-e', '--exclude', dest='exclude_list', | |
help='Exclude certain exceptions from output.', | |
default="", | |
metavar='Exception,Exception,...') | |
options, args = parser.parse_args() | |
bufMode = False | |
buf = '' | |
errors = [] | |
with open(options.file, 'r') as f: | |
for line in f: | |
if 'Traceback' in line: | |
bufMode = True | |
continue | |
# Usually a Traceback includes a new line at the end, therefore | |
# a check for line length should be safe. However this might bite | |
# you ;-) | |
if line and len(line) < 5: | |
bufMode = False | |
errors.append(buf) | |
buf = '' | |
if bufMode: | |
# Truncate lines longer than 400 characters. | |
if len(line) > 400: | |
line = line[:400]+'...\n' | |
buf += line | |
unique_errs = set(errors) | |
excludes = [] | |
if options.exclude_list: | |
excludes = options.exclude_list.split(',') | |
for err in unique_errs: | |
if any([excl in err for excl in excludes]): | |
continue | |
print err | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very nice.
I added an additional snippet to check what kind of raised error it is and the call stack by processing File.
Do you think that will be useful to add as a snippet to your gist?