forked from dabeaz/generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apachelog.py
37 lines (25 loc) · 898 Bytes
/
apachelog.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
# apachelog.py
#
# Parse an apache log file into a sequence of dictionaries
from fieldmap import field_map
import re
logpats = r'(\S+) (\S+) (\S+) \[(.*?)\] ' \
r'"(\S+) (\S+) (\S+)" (\S+) (\S+)'
logpat = re.compile(logpats)
def apache_log(lines):
groups = (logpat.match(line) for line in lines)
tuples = (g.groups() for g in groups if g)
colnames = ('host','referrer','user','datetime',
'method', 'request','proto','status','bytes')
log = (dict(zip(colnames,t)) for t in tuples)
log = field_map(log,"status",int)
log = field_map(log,"bytes",
lambda s: int(s) if s != '-' else 0)
return log
# Example use:
if __name__ == '__main__':
from linesdir import lines_from_dir
lines = lines_from_dir("access-log*","www")
log = apache_log(lines)
for r in log:
print(r)