-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogAnalyze.py
executable file
·117 lines (105 loc) · 3.88 KB
/
LogAnalyze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""Går igenom en logg från en typisk körning och skapar HTML-filer om
vad som gått bra och dåligt (en övergripande vy och en sida per
modul/operation)"""
import re,sys,codecs,locale
from sets import Set
locale.setlocale(locale.LC_ALL,'')
if sys.platform == 'win32':
if sys.stdout.encoding:
defaultencoding = sys.stdout.encoding
else:
defaultencoding = 'cp850'
else:
if sys.stdout.encoding:
defaultencoding = sys.stdout.encoding
else:
defaultencoding = locale.getpreferredencoding()
# print "setting sys.stdout to a '%s' writer" % defaultencoding
sys.stdout = codecs.getwriter(defaultencoding)(sys.__stdout__, 'replace')
sys.stderr = codecs.getwriter(defaultencoding)(sys.__stderr__, 'replace')
re_logline = re.compile(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\w+) +\(([a-zA-Z0-9_, ]*)\) (\w+): ([^:]+)(: ([^\r]*)|)').match
re_tbframe = re.compile(r' File "([^"]+)", line (\d+), in (\w+)').match
re_msg = re.compile(r'')
def analyse(logfile):
f = codecs.open(logfile,encoding='utf-8')
errcnt = 0
warncnt = 0
okcnt = 0
errlocs = {}
warnlocs = {}
probdocs = Set()
for line in f:
# sys.stdout.write(line)
log_match = re_logline(line)
if not log_match:
sys.stdout.write(u"Malformed log line: %r\n" % line)
else:
dt = log_match.group(1)
module = log_match.group(2)
loc = log_match.group(3)
level = log_match.group(4)
docid = log_match.group(5)
message = log_match.group(7)
if message:
message = re.sub(r'\[[^]]+\]', r'[...]',message)
else:
continue
if level == 'ERROR' and 'Error:' in line: # real errors with tracebacks
# print log_match.groups()
probdocs.add(docid)
errcnt += 1
#print "ERR: %s" % message
done = False
while not done:
line = f.next()
if 'Error: ' in line or 'Error [' in line:
done = True
# sys.stdout.write(" TB: " + line)
fb_match = re_tbframe(line)
if fb_match:
errloc = "%s:%s" % (fb_match.group(1), fb_match.group(2))
else:
errcode = line
#print "Done"
#errloc += "(%s)" % line.split(":")[0]
errloc += "(%s)" % line[:-2]
errlocs[errloc] = errlocs.get(errloc,0) + 1
elif level == 'ERROR':
probdocs.add(docid)
errcnt += 1
errloc = "%s (%s)" % (loc,message)
# print "ERRLOC %r" % errloc
errlocs[errloc] = errlocs.get(errloc,0) + 1
elif level == 'WARNING':
probdocs.add(docid)
warncnt += 1
warnloc = "%s (%s)" % (loc,message)
# print "WARNLOC %r" % warnloc
warnlocs[warnloc] = warnlocs.get(warnloc,0) + 1
elif level == 'INFO':
if 'OK' in message:
okcnt += 1
print "%s errors, %s warnings, %s ok" % (errcnt, warncnt, okcnt)
print "ERRORS"
printdict(errlocs)
print "WARNINGS"
printdict(warnlocs)
dumplist(probdocs, "probdocs.txt")
def dumplist(l, file):
fp = codecs.open(file, "w",encoding='utf-8')
# fp = open(file,"w")
for entry in l:
fp.write(entry+"\n")
fp.close()
def printdict(d):
items = d.items()
items = [(v, k) for (k, v) in items]
items.sort()
items.reverse() # so largest is first
items = [(k, v) for (v, k) in items]
for k,v in items:
print u" %s: %s occurrences" % (k,v)
if __name__ == "__main__":
analyse(sys.argv[1])