diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2015-02-09 19:49:00 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2015-02-09 19:49:00 (GMT) |
commit | 365701add94255d753d555c6b3833dd8cc6d43a0 (patch) | |
tree | b4f6639641bfd98ca65bb73ef2227351d5c317ed /Lib | |
parent | 438f9134cfb7a3b68cff9de9f730f42f68c2cc94 (diff) | |
download | cpython-365701add94255d753d555c6b3833dd8cc6d43a0.zip cpython-365701add94255d753d555c6b3833dd8cc6d43a0.tar.gz cpython-365701add94255d753d555c6b3833dd8cc6d43a0.tar.bz2 |
Added respect_handler_level to QueueListener.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/handlers.py | 14 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 19 |
2 files changed, 29 insertions, 4 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index c67ac99..d4f8aef 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1,4 +1,4 @@ -# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, @@ -18,7 +18,7 @@ Additional handlers for the logging package for Python. The core package is based on PEP 282 and comments thereto in comp.lang.python. -Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved. +Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved. To use, simply 'import logging.handlers' and log away! """ @@ -1350,7 +1350,7 @@ if threading: """ _sentinel = None - def __init__(self, queue, *handlers): + def __init__(self, queue, *handlers, respect_handler_level=False): """ Initialise an instance with the specified queue and handlers. @@ -1359,6 +1359,7 @@ if threading: self.handlers = handlers self._stop = threading.Event() self._thread = None + self.respect_handler_level = respect_handler_level def dequeue(self, block): """ @@ -1399,7 +1400,12 @@ if threading: """ record = self.prepare(record) for handler in self.handlers: - handler.handle(record) + if not self.respect_handler_level: + process = True + else: + process = record.levelno >= handler.level + if process: + handler.handle(record) def _monitor(self): """ diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 9674322..c8b6a98 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -3006,6 +3006,25 @@ class QueueHandlerTest(BaseTest): self.assertTrue(handler.matches(levelno=logging.WARNING, message='1')) self.assertTrue(handler.matches(levelno=logging.ERROR, message='2')) self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='3')) + handler.close() + + # Now test with respect_handler_level set + + handler = support.TestHandler(support.Matcher()) + handler.setLevel(logging.CRITICAL) + listener = logging.handlers.QueueListener(self.queue, handler, + respect_handler_level=True) + listener.start() + try: + self.que_logger.warning(self.next_message()) + self.que_logger.error(self.next_message()) + self.que_logger.critical(self.next_message()) + finally: + listener.stop() + self.assertFalse(handler.matches(levelno=logging.WARNING, message='4')) + self.assertFalse(handler.matches(levelno=logging.ERROR, message='5')) + self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6')) + ZERO = datetime.timedelta(0) |