Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 365701a

Browse files
committed
Added respect_handler_level to QueueListener.
1 parent 438f913 commit 365701a

5 files changed

Lines changed: 50 additions & 6 deletions

File tree

Doc/howto/logging-cookbook.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ which, when run, will produce::
325325

326326
MainThread: Look out!
327327

328+
.. versionchanged:: 3.5
329+
Prior to Python 3.5, the :class:`QueueListener` always passed every message
330+
received from the queue to every handler it was initialized with. (This was
331+
because it was assumed that level filtering was all done on the other side,
332+
where the queue is filled.) From 3.5 onwards, this behaviour can be changed
333+
by passing a keyword argument ``respect_handler_level=True`` to the
334+
listener's constructor. When this is done, the listener compares the level
335+
of each message with the handler's level, and only passes a message to a
336+
handler if it's appropriate to do so.
328337

329338
.. _network-logging:
330339

Doc/library/logging.handlers.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,13 +953,20 @@ applications where threads servicing clients need to respond as quickly as
953953
possible, while any potentially slow operations (such as sending an email via
954954
:class:`SMTPHandler`) are done on a separate thread.
955955

956-
.. class:: QueueListener(queue, *handlers)
956+
.. class:: QueueListener(queue, *handlers, respect_handler_level=False)
957957

958958
Returns a new instance of the :class:`QueueListener` class. The instance is
959959
initialized with the queue to send messages to and a list of handlers which
960960
will handle entries placed on the queue. The queue can be any queue-
961961
like object; it's passed as-is to the :meth:`dequeue` method, which needs
962-
to know how to get messages from it.
962+
to know how to get messages from it. If ``respect_handler_level`` is ``True``,
963+
a handler's level is respected (compared with the level for the message) when
964+
deciding whether to pass messages to that handler; otherwise, the behaviour
965+
is as in previous Python versions - to always pass each message to each
966+
handler.
967+
968+
.. versionchanged:: 3.5
969+
The ``respect_handler_levels`` argument was added.
963970

964971
.. method:: dequeue(block)
965972

Lib/logging/handlers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818
Additional handlers for the logging package for Python. The core package is
1919
based on PEP 282 and comments thereto in comp.lang.python.
2020
21-
Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
@@ -1350,7 +1350,7 @@ class QueueListener(object):
13501350
"""
13511351
_sentinel = None
13521352

1353-
def __init__(self, queue, *handlers):
1353+
def __init__(self, queue, *handlers, respect_handler_level=False):
13541354
"""
13551355
Initialise an instance with the specified queue and
13561356
handlers.
@@ -1359,6 +1359,7 @@ def __init__(self, queue, *handlers):
13591359
self.handlers = handlers
13601360
self._stop = threading.Event()
13611361
self._thread = None
1362+
self.respect_handler_level = respect_handler_level
13621363

13631364
def dequeue(self, block):
13641365
"""
@@ -1399,7 +1400,12 @@ def handle(self, record):
13991400
"""
14001401
record = self.prepare(record)
14011402
for handler in self.handlers:
1402-
handler.handle(record)
1403+
if not self.respect_handler_level:
1404+
process = True
1405+
else:
1406+
process = record.levelno >= handler.level
1407+
if process:
1408+
handler.handle(record)
14031409

14041410
def _monitor(self):
14051411
"""

Lib/test/test_logging.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,25 @@ def test_queue_listener(self):
30063006
self.assertTrue(handler.matches(levelno=logging.WARNING, message='1'))
30073007
self.assertTrue(handler.matches(levelno=logging.ERROR, message='2'))
30083008
self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='3'))
3009+
handler.close()
3010+
3011+
# Now test with respect_handler_level set
3012+
3013+
handler = support.TestHandler(support.Matcher())
3014+
handler.setLevel(logging.CRITICAL)
3015+
listener = logging.handlers.QueueListener(self.queue, handler,
3016+
respect_handler_level=True)
3017+
listener.start()
3018+
try:
3019+
self.que_logger.warning(self.next_message())
3020+
self.que_logger.error(self.next_message())
3021+
self.que_logger.critical(self.next_message())
3022+
finally:
3023+
listener.stop()
3024+
self.assertFalse(handler.matches(levelno=logging.WARNING, message='4'))
3025+
self.assertFalse(handler.matches(levelno=logging.ERROR, message='5'))
3026+
self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6'))
3027+
30093028

30103029
ZERO = datetime.timedelta(0)
30113030

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- logging.handlers.QueueListener now takes a respect_handler_level keyword
17+
argument which, if set to True, will pass messages to handlers taking handler
18+
levels into account.
1619

1720
What's New in Python 3.5 alpha 1?
1821
=================================

0 commit comments

Comments
 (0)