diff --git a/Logger/LogBuffer.py b/Logger/LogBuffer.py index 5b45c32..6b7a337 100644 --- a/Logger/LogBuffer.py +++ b/Logger/LogBuffer.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import os, re, json -from LogFilter import LogFilter, LogFilterSet +import os, re, json, logging +from LogFilter import LogFilter, LogFilterSet, LogSkipException class LogBuffer(): @@ -11,8 +11,10 @@ def __init__(self, filters): self.stack = [] def append(self, string): - self.stack.append(self.filters.apply(string)) - #print self.filters.apply(string) + try: + self.stack.append(self.filters.apply(string)) + except LogSkipException as skip: + logging.debug(str(skip)) def push(self, string): self.temp += string @@ -32,4 +34,3 @@ def pop(self): if self.stack: return self.stack.pop(0) - diff --git a/Logger/LogFilter.py b/Logger/LogFilter.py index 48f95b0..25ddbb8 100644 --- a/Logger/LogFilter.py +++ b/Logger/LogFilter.py @@ -1,16 +1,27 @@ import os, re, json +class LogSkipException(Exception): + + def __init__(self, pattern, message): + self.expression = pattern + self.message = message + super(Exception, self).__init__('[SKIP] pattern: "'+str(pattern)+'", string: "'+message+'"') + + class LogFilter(): def __init__(self, attributes): - self.pattern = attributes.pop('pattern') - self.attributes = attributes + self.pattern = attributes.pop('pattern') self.regex = re.compile(self.pattern) - print(self.attributes) - print(self.to_json('blah')) + self.action = getattr(self.regex, attributes.pop('action', 'match'), self.regex.match) + self.serialize = self.skip if attributes.pop('skip', False) else self.to_json + self.attributes = attributes - def match(self, string): - return self.regex.match(string) + def skip(self, string): + raise LogSkipException(self.pattern, string) + + def apply(self, string): + return self.action(string) def to_json(self, string): tmp = {'string':string} @@ -22,16 +33,19 @@ class DummyLogFilter(LogFilter): def __init__(self, pattern): self.attributes = {} + self.serialize = self.to_json - def match(self, string): + def apply(self, string): return True class LogFilterSet(): def __init__(self, filters): - self.filters = filters + self.filters = filters or [] self.filters.append(DummyLogFilter(pattern="*")) def apply(self, string): for filter in self.filters: - if filter.match(string): return filter.to_json(string) + if filter.apply(string): + return filter.serialize(string) + diff --git a/test/config.yaml b/test/config.yaml index 4444f1b..e83c49d 100644 --- a/test/config.yaml +++ b/test/config.yaml @@ -1,13 +1,12 @@ { - "host": "im.srv.cesga.es", + "host": "localhost", "port": 5672, - "user": "mso4sc", - "pass": "remotelogger", + "user": "guest", + "pass": "guest", "exchange": "exchange", "exchange_type": "direct", - "routing_key": "routing_key2", - "queue": "queue11", + "routing_key": "routing_key", + "queue": "queue", "heartbeat": 0, "blocked_connection_timeout": 300 } -