Skip to content
Snippets Groups Projects
Commit fdf09f0d authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: make handler to logging filter

parent 283f8a3d
No related branches found
No related tags found
1 merge request!22Release 0.3
...@@ -55,7 +55,7 @@ from .datainconsistency import DataInconsistencyError ...@@ -55,7 +55,7 @@ from .datainconsistency import DataInconsistencyError
from .datamodel_problems import DataModelProblems from .datamodel_problems import DataModelProblems
from .guard import RETRIEVE, ProhibitedException from .guard import RETRIEVE, ProhibitedException
from .guard import global_guard as guard from .guard import global_guard as guard
from .suppressable import Suppressable from .suppressKnown import SuppressKnown
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -96,12 +96,12 @@ class Crawler(object): ...@@ -96,12 +96,12 @@ class Crawler(object):
self.debug_file = debug_file self.debug_file = debug_file
self.abort_on_exception = abort_on_exception self.abort_on_exception = abort_on_exception
self.update_cache = UpdateCache() self.update_cache = UpdateCache()
self.filterKnown = SuppressKnown()
self.sup = Suppressable(logger=logger) logger.addFilter(self.filterKnown)
if hideKnown is False: if hideKnown is False:
for cat in ["matches", "inconsistency"]: for cat in ["matches", "inconsistency"]:
self.sup.reset(cat) self.filterKnown.reset(cat)
if self.use_cache: if self.use_cache:
self.cache = Cache(db_file=cache_file) self.cache = Cache(db_file=cache_file)
...@@ -224,7 +224,8 @@ class Crawler(object): ...@@ -224,7 +224,8 @@ class Crawler(object):
msg = ("The crawler has no matching rules for and is thus " msg = ("The crawler has no matching rules for and is thus "
"ignoring:\n{}".format(item)) "ignoring:\n{}".format(item))
self.sup.warning(msg, identifier=item, category="matches") logging.warning(msg, extra={"identifier": str(item),
'category': "matches"})
if len(matches[idx]) > 1: if len(matches[idx]) > 1:
msg = ("Attention: More than one matching cfood!\n" msg = ("Attention: More than one matching cfood!\n"
...@@ -232,7 +233,8 @@ class Crawler(object): ...@@ -232,7 +233,8 @@ class Crawler(object):
+ "\tRecordTypes:\t" + ", ".join( + "\tRecordTypes:\t" + ", ".join(
matches[idx])+"\n") matches[idx])+"\n")
self.sup.warning(msg, identifier=item, category="matches") logging.warning(msg, extra={"identifier": str(item),
'category': "matches"})
def cached_find_identifiables(self, identifiables): def cached_find_identifiables(self, identifiables):
if self.use_cache: if self.use_cache:
......
...@@ -6,18 +6,28 @@ import sqlite3 ...@@ -6,18 +6,28 @@ import sqlite3
from hashlib import sha256 from hashlib import sha256
class Suppressable(object): class SuppressKnown(logging.Filter):
"""
This filter allows to suppress log messages that were shown before.
The python logging module can be used as normal. This Filter needs to be
added to the appropriate Logger and logging calls (e.g. to warning, info
etc.) need to have an additional `extra` argument.
This argument should be a dict that contains an identifier and a category.
Example: `extra={"identifier":"<Record>something</Record>",
category="entities"}
The identifier is used to check whether a message was shown before and
should be a string. The category can be used to remove a specific group of
messages from memory and the logger would show those messages again even
when they are known.
"""
def __init__(self, db_file=None, logger=None): def __init__(self, db_file=None, logger=None):
if db_file: if db_file:
self.db_file = db_file self.db_file = db_file
else: else:
self.db_file = "/tmp/caosadvanced_suppressed_cache.db" self.db_file = "/tmp/caosadvanced_suppressed_cache.db"
if logger:
self.logger = logger
else:
self.logger = logging.getLogger()
if not os.path.exists(self.db_file): if not os.path.exists(self.db_file):
self.create_cache() self.create_cache()
...@@ -57,32 +67,19 @@ class Suppressable(object): ...@@ -57,32 +67,19 @@ class Suppressable(object):
else: else:
return True return True
def warning(self, txt, identifier, category):
self.msg(txt, identifier, "warning", category)
def debug(self, txt, identifier, category):
self.msg(txt, identifier, "debug", category)
def info(self, txt, identifier, category):
self.msg(txt, identifier, "info", category)
def error(self, txt, identifier, category):
self.msg(txt, identifier, "error", category)
def hash(self, txt, identifier): def hash(self, txt, identifier):
return sha256((txt+str(identifier)).encode("utf-8")).hexdigest() return sha256((txt+str(identifier)).encode("utf-8")).hexdigest()
def msg(self, txt, identifier, kind, category): def filter(self, record):
if self.was_tagged(self.hash(txt, identifier)): if not hasattr(record, "identifier"):
return return 1
if not hasattr(record, "category"):
return 1
if self.was_tagged(self.hash(record.getMessage(), record.identifier)):
return 0
if kind == "debug": self.tag_msg(record.getMessage(), record.identifier, record.category)
self.logger.debug(txt)
elif kind == "info":
self.logger.info(txt)
elif kind == "warning":
self.logger.warning(txt)
elif kind == "error":
self.logger.error(txt)
self.tag_msg(txt, identifier, category) return 1
...@@ -27,20 +27,29 @@ import os ...@@ -27,20 +27,29 @@ import os
import unittest import unittest
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from caosadvancedtools.suppressable import Suppressable from caosadvancedtools.suppressKnown import SuppressKnown
class Record(object):
identifier = "5"
category = "test"
def getMessage(self):
return "hi"
class SupTestBasic(unittest.TestCase): class SupTestBasic(unittest.TestCase):
def setUp(self): def setUp(self):
self.db_file = "/tmp/test_suppress_msg_db_file.db" self.db_file = "/tmp/test_suppress_msg_db_file.db"
self.basic = Suppressable(db_file=self.db_file) self.basic = SuppressKnown(db_file=self.db_file)
def test_msg(self): def test_msg(self):
r = Record()
# testing basic setup # testing basic setup
self.basic.info("hi", 5, "test") self.basic.filter(r)
digest = self.basic.hash("hi", 5) digest = self.basic.hash("hi", 5)
assert self.basic.was_tagged(digest) assert self.basic.was_tagged(digest)
self.basic.info("hi", 5, "test") self.basic.filter(r)
def tearDown(self): def tearDown(self):
os.remove(self.db_file) os.remove(self.db_file)
...@@ -49,7 +58,7 @@ class SupTestBasic(unittest.TestCase): ...@@ -49,7 +58,7 @@ class SupTestBasic(unittest.TestCase):
class SupTestAdvanced(SupTestBasic): class SupTestAdvanced(SupTestBasic):
def setUp(self): def setUp(self):
self.db_file = "/tmp/test_suppress_msg_db_file.db" self.db_file = "/tmp/test_suppress_msg_db_file.db"
self.basic = Suppressable(db_file=self.db_file) self.basic = SuppressKnown(db_file=self.db_file)
def test_logger(self): def test_logger(self):
""" """
...@@ -60,10 +69,10 @@ class SupTestAdvanced(SupTestBasic): ...@@ -60,10 +69,10 @@ class SupTestAdvanced(SupTestBasic):
logger = logging.getLogger() logger = logging.getLogger()
logger.addHandler(logging.FileHandler(logfile.name)) logger.addHandler(logging.FileHandler(logfile.name))
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
sup = SuppressKnown(db_file=self.db_file)
logger.addFilter(sup)
suppressable = Suppressable(db_file=self.db_file, logger=logger) logger.info("hi", extra={"identifier": "5", 'category': "test"})
suppressable.info("hi", 5, "test")
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
# assert that the log was written # assert that the log was written
...@@ -72,7 +81,7 @@ class SupTestAdvanced(SupTestBasic): ...@@ -72,7 +81,7 @@ class SupTestAdvanced(SupTestBasic):
assert log.count("\n") == 1 assert log.count("\n") == 1
# the following is unchanged and should be suppressed # the following is unchanged and should be suppressed
suppressable.info("hi", 5, "test") logger.info("hi", extra={"identifier": "5", 'category': "test"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
# one line with one hi # one line with one hi
...@@ -80,21 +89,21 @@ class SupTestAdvanced(SupTestBasic): ...@@ -80,21 +89,21 @@ class SupTestAdvanced(SupTestBasic):
assert log.count("\n") == 1 assert log.count("\n") == 1
# the following is a new message and should thus not be suppressed # the following is a new message and should thus not be suppressed
suppressable.info("ha", 5, "new") logger.info("ha", extra={"identifier": "5", 'category': "new"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
assert log.count("ha") == 1 assert log.count("ha") == 1
assert log.count("\n") == 2 assert log.count("\n") == 2
# the following has a identifier and should thus not be suppressed # the following has a identifier and should thus not be suppressed
suppressable.info("hi", 6, "test") logger.info("hi", extra={"identifier": "6", 'category': "test"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
assert log.count("hi") == 2 assert log.count("hi") == 2
assert log.count("\n") == 3 assert log.count("\n") == 3
# the following should be suppressed again # the following should be suppressed again
suppressable.info("ha", 5, "new") logger.info("ha", extra={"identifier": "5", 'category': "new"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
assert log.count("ha") == 1 assert log.count("ha") == 1
...@@ -102,17 +111,17 @@ class SupTestAdvanced(SupTestBasic): ...@@ -102,17 +111,17 @@ class SupTestAdvanced(SupTestBasic):
assert log.count("\n") == 3 assert log.count("\n") == 3
# resetting test category; hi should be removed # resetting test category; hi should be removed
suppressable.reset("test") sup.reset("test")
# hi should not be suppressed # hi should not be suppressed
suppressable.info("hi", 5, "test") logger.info("hi", extra={"identifier": "5", 'category': "test"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
assert log.count("hi") == 3 assert log.count("hi") == 3
assert log.count("\n") == 4 assert log.count("\n") == 4
# the following should be suppressed still # the following should be suppressed still
suppressable.info("ha", 5, "new") logger.info("ha", extra={"identifier": "5", 'category': "new"})
with open(logfile.name) as lf: with open(logfile.name) as lf:
log = lf.read() log = lf.read()
assert log.count("ha") == 1 assert log.count("ha") == 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment