Created
June 17, 2021 23:23
-
-
Save Habetdin/b8ded9c0daff4e3ef6aa2d14256be4d4 to your computer and use it in GitHub Desktop.
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
# | |
# Extracts exceptions from log files. | |
# | |
import sys | |
import re | |
from collections import defaultdict | |
REGEX = re.compile("(^\tat |^Caused by: |^\t... \\d+ more)") | |
# Usually, all inner lines of a stack trace will be "at" or "Caused by" lines. | |
# With one exception: the line following a "nested exception is" line does not | |
# follow that convention. Due to that, this line is handled separately. | |
CONT = re.compile("; nested exception is: *$") | |
exceptions = defaultdict(int) | |
def registerException(exc): | |
exceptions[exc] += 1 | |
def processFile(fileName): | |
with open(fileName, "r", encoding='utf-8') as fh: | |
currentMatch = None | |
lastLine = None | |
addNextLine = False | |
line = fh.readline() | |
while line: | |
if addNextLine and currentMatch != None: | |
addNextLine = False | |
currentMatch += line | |
continue | |
match = REGEX.search(line) != None | |
if match and currentMatch != None: | |
currentMatch += line | |
elif match: | |
currentMatch = lastLine + line | |
else: | |
if currentMatch != None: | |
registerException(currentMatch) | |
currentMatch = None | |
lastLine = line | |
addNextLine = CONT.search(line) != None | |
line = fh.readline() | |
# If last line in file was a stack trace | |
if currentMatch != None: | |
registerException(currentMatch) | |
for f in sys.argv[1:]: | |
processFile(f) | |
for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True): | |
print(item[1], ":", item[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment