Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip log lines #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Logger/LogBuffer.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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
Expand All @@ -32,4 +34,3 @@ def pop(self):
if self.stack:
return self.stack.pop(0)


32 changes: 23 additions & 9 deletions Logger/LogFilter.py
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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)

11 changes: 5 additions & 6 deletions test/config.yaml
Original file line number Diff line number Diff line change
@@ -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
}