logfilter: Only filter debug messages

This commit is contained in:
Florian Bruhin 2014-06-06 15:48:42 +02:00
parent 3ceeccb133
commit 9c9492a706
3 changed files with 23 additions and 5 deletions

View File

@ -35,7 +35,8 @@ def _parse_args():
parser.add_argument('-l', '--loglevel', dest='loglevel',
help="Set loglevel", default='info')
parser.add_argument('--logfilter',
help="Comma-separated list of things to be logged")
help="Comma-separated list of things to be logged "
"to the debug log on stdout.")
parser.add_argument('-c', '--confdir', help="Set config directory (empty "
"for no config storage)")
parser.add_argument('--debug', help="Turn on debugging options.",

View File

@ -35,10 +35,10 @@ class LogFilterTests(TestCase):
def setUp(self):
self.logger = logging.getLogger("foo")
def _make_record(self, name):
def _make_record(self, name, level=logging.INFO):
"""Create a bogus logging record with the supplied logger name."""
return self.logger.makeRecord(name, level=logging.DEBUG, fn=None,
lno=0, msg="", args=None, exc_info=None)
return self.logger.makeRecord(name, level=level, fn=None, lno=0,
msg="", args=None, exc_info=None)
def test_empty(self):
"""Test if an empty filter lets all messages through."""
@ -78,6 +78,20 @@ class LogFilterTests(TestCase):
record = self._make_record("spam.ham.salami")
self.assertTrue(logfilter.filter(record))
def test_debug(self):
"""Test if debug messages are never filtered."""
logfilter = log.LogFilter(["eggs"])
# First check if the filter works as intended with non-debug messages
record = self._make_record("eggs", level=logging.INFO)
self.assertTrue(logfilter.filter(record))
record = self._make_record("bacon", level=logging.INFO)
self.assertFalse(logfilter.filter(record))
# Then check if debug is not filtered
record = self._make_record("eggs", level=logging.DEBUG)
self.assertTrue(logfilter.filter(record))
record = self._make_record("bacon", level=logging.DEBUG)
self.assertTrue(logfilter.filter(record))
class RAMHandlerTests(TestCase):

View File

@ -82,7 +82,7 @@ def init_log(args):
raise ValueError("Invalid log level: {}".format(args.loglevel))
console, ram = _init_handlers(numeric_level, args.color)
if args.logfilter is not None:
if args.logfilter is not None and numeric_level <= logging.DEBUG:
console.addFilter(args.logfilter.split(','))
root = getLogger()
root.addHandler(console)
@ -216,6 +216,9 @@ class LogFilter(logging.Filter):
"""Determine if the specified record is to be logged."""
if self.names is None:
return True
if record.levelno <= logging.DEBUG:
# DEBUG or more important, so we won't filter at all
return True
for name in self.names:
if record.name == name:
return True