X-Git-Url: http://git.tremily.us/?p=apachelog.git;a=blobdiff_plain;f=apachelog%2Fprocessor%2F__init__.py;fp=apachelog%2Fprocessor%2F__init__.py;h=29f2ba10b0c5a781805aa42e4af65bfabde44518;hp=0000000000000000000000000000000000000000;hb=a901709d6142b8f9528eed5e801e12bd54da68fe;hpb=770e304fecbbc6c2ee30a5df68c53fb37756f5f2 diff --git a/apachelog/processor/__init__.py b/apachelog/processor/__init__.py new file mode 100644 index 0000000..29f2ba1 --- /dev/null +++ b/apachelog/processor/__init__.py @@ -0,0 +1,38 @@ +"""Define ``Processor`` classes for aggregating data across log files. +""" + +class Processor (object): + def process(self, data): + pass + + +def process(stream, parser, processors): + r"""Process a log with a list of processors. + + For each line in the log located at ``filename``, parse the line + using ``parser`` and analyze it with each of the ``Processor`` + instances in the list ``processors``. + + >>> import StringIO + >>> from apachelog.parser import Parser, FORMATS + >>> class PrinthostProcessor (Processor): + ... def __init__(self, name): + ... self.name = name + ... def process(self, data): + ... print('{}: {}'.format(self.name, data['%h'])) + >>> stream = StringIO.StringIO('\n'.join([ + ... '192.168.0.1 - - [18/Feb/2012:10:25:43 -0500] "GET / HTTP/1.1" 200 561 "-" "Mozilla/5.0 (...)"', + ... '192.168.0.2 - - [18/Feb/2012:10:25:58 -0500] "GET / HTTP/1.1" 200 561 "-" "Mozilla/5.0 (...)"', + ... ])) + >>> parser = Parser(FORMATS['extended']) + >>> processors = [PrinthostProcessor('a'), PrinthostProcessor('b')] + >>> process(stream, parser, processors) + a: 192.168.0.1 + b: 192.168.0.1 + a: 192.168.0.2 + b: 192.168.0.2 + """ + for line in stream: + data = parser.parse(line) + for processor in processors: + processor.process(data)