diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-10-19 20:53:01 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-10-19 20:53:01 (GMT) |
commit | 6dbed2e8b3fb90b25e2b99c5e522a5205626d07e (patch) | |
tree | 76ceee4dbccb88e720729fa53428838981429998 /Lib/test/test_logging.py | |
parent | 6fac8171367e0f2626613eb0ac07fc79c25f2500 (diff) | |
download | cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.zip cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.tar.gz cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.tar.bz2 |
logging: Allowed filters to be just callables.
Diffstat (limited to 'Lib/test/test_logging.py')
-rw-r--r-- | Lib/test/test_logging.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index ea2ea2e..a738d7a 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -309,6 +309,35 @@ class BasicFilterTest(BaseTest): finally: handler.removeFilter(filter_) + def test_callable_filter(self): + # Only messages satisfying the specified criteria pass through the + # filter. + + def filterfunc(record): + parts = record.name.split('.') + prefix = '.'.join(parts[:2]) + return prefix == 'spam.eggs' + + handler = self.root_logger.handlers[0] + try: + handler.addFilter(filterfunc) + spam = logging.getLogger("spam") + spam_eggs = logging.getLogger("spam.eggs") + spam_eggs_fish = logging.getLogger("spam.eggs.fish") + spam_bakedbeans = logging.getLogger("spam.bakedbeans") + + spam.info(self.next_message()) + spam_eggs.info(self.next_message()) # Good. + spam_eggs_fish.info(self.next_message()) # Good. + spam_bakedbeans.info(self.next_message()) + + self.assert_log_lines([ + ('spam.eggs', 'INFO', '2'), + ('spam.eggs.fish', 'INFO', '3'), + ]) + finally: + handler.removeFilter(filterfunc) + # # First, we define our levels. There can be as many as you want - the only |