|
1 """ |
|
2 Parse log data into log_events |
|
3 """ |
|
4 |
|
5 import datetime |
|
6 |
|
7 import log_line |
|
8 from log_line import LogTypes |
|
9 |
|
10 class LogParser (object) : |
|
11 """ |
|
12 Abstract interface |
|
13 """ |
|
14 |
|
15 def __init__ (self, tz, timestamp_fmt="%H:%M:%S") : |
|
16 """ |
|
17 Setup the parser to use the given format for line timestamps, which are of the given timezone |
|
18 """ |
|
19 |
|
20 self.tz = tz |
|
21 self.timestamp_fmt = timestamp_fmt |
|
22 |
|
23 def parse_lines (self, lines, date=None) : |
|
24 """ |
|
25 Parse the given (iterable) lines of unicode text into a LogEvent, no trailing newline. |
|
26 |
|
27 Giving date lets the parser build full timestamps, otherwise, unless line timestamps have full date |
|
28 information, event timestamps will have a date component of 1900/1/1. |
|
29 """ |
|
30 |
|
31 abstract |
|
32 |
|
33 |
|
34 class IrssiParser (LogParser) : |
|
35 """ |
|
36 A parser for irssi logfiles |
|
37 """ |
|
38 |
|
39 def parse_lines (self, lines, date=None) : |
|
40 """ |
|
41 Parse the given lines, yielding LogEvents. |
|
42 """ |
|
43 |
|
44 for line in lines : |
|
45 # status lines |
|
46 if line.startswith('---') : |
|
47 # XXX: handle these |
|
48 continue |
|
49 |
|
50 # normal lines |
|
51 else : |
|
52 # XXX: only parse timestamps for now |
|
53 timestamp, data = line.split(' ', 1) |
|
54 |
|
55 # parse timestamp into naive datetime |
|
56 dt = datetime.datetime.strptime(timestamp, self.timestamp_fmt) |
|
57 |
|
58 # override date? |
|
59 if date : |
|
60 dt = dt.replace(year=date.year, month=date.month, day=date.day) |
|
61 |
|
62 # now localize with timezone |
|
63 dtz = self.tz.localize(dt) |
|
64 |
|
65 # yield raw events |
|
66 yield log_line.LogLine(LogTypes.RAW, dtz, None, data) |
|
67 |