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

Skip to content

Commit cccf606

Browse files
committed
Closes #26559: Allow configuring flush-on-close behaviour of MemoryHandler.
1 parent d141531 commit cccf606

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

Doc/library/logging.handlers.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,18 @@ should, then :meth:`flush` is expected to do the flushing.
806806
overridden to implement custom flushing strategies.
807807

808808

809-
.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None)
809+
.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)
810810

811811
Returns a new instance of the :class:`MemoryHandler` class. The instance is
812812
initialized with a buffer size of *capacity*. If *flushLevel* is not specified,
813813
:const:`ERROR` is used. If no *target* is specified, the target will need to be
814-
set using :meth:`setTarget` before this handler does anything useful.
814+
set using :meth:`setTarget` before this handler does anything useful. If
815+
*flushOnClose* is specified as ``False``, then the buffer is *not* flushed when
816+
the handler is closed. If not specified or specified as ``True``, the previous
817+
behaviour of flushing the buffer will occur when the handler is closed.
818+
819+
.. versionchanged:: 3.6
820+
The *flushOnClose* parameter was added.
815821

816822

817823
.. method:: close()

Lib/logging/handlers.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2016 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-2015 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
@@ -1238,17 +1238,25 @@ class MemoryHandler(BufferingHandler):
12381238
flushing them to a target handler. Flushing occurs whenever the buffer
12391239
is full, or when an event of a certain severity or greater is seen.
12401240
"""
1241-
def __init__(self, capacity, flushLevel=logging.ERROR, target=None):
1241+
def __init__(self, capacity, flushLevel=logging.ERROR, target=None,
1242+
flushOnClose=True):
12421243
"""
12431244
Initialize the handler with the buffer size, the level at which
12441245
flushing should occur and an optional target.
12451246
12461247
Note that without a target being set either here or via setTarget(),
12471248
a MemoryHandler is no use to anyone!
1249+
1250+
The ``flushOnClose`` argument is ``True`` for backward compatibility
1251+
reasons - the old behaviour is that when the handler is closed, the
1252+
buffer is flushed, even if the flush level hasn't been exceeded nor the
1253+
capacity exceeded. To prevent this, set ``flushOnClose`` to ``False``.
12481254
"""
12491255
BufferingHandler.__init__(self, capacity)
12501256
self.flushLevel = flushLevel
12511257
self.target = target
1258+
# See Issue #26559 for why this has been added
1259+
self.flushOnClose = flushOnClose
12521260

12531261
def shouldFlush(self, record):
12541262
"""
@@ -1282,10 +1290,12 @@ def flush(self):
12821290

12831291
def close(self):
12841292
"""
1285-
Flush, set the target to None and lose the buffer.
1293+
Flush, if appropriately configured, set the target to None and lose the
1294+
buffer.
12861295
"""
12871296
try:
1288-
self.flush()
1297+
if self.flushOnClose:
1298+
self.flush()
12891299
finally:
12901300
self.acquire()
12911301
try:

Lib/test/test_logging.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ class MemoryHandlerTest(BaseTest):
958958
def setUp(self):
959959
BaseTest.setUp(self)
960960
self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
961-
self.root_hdlr)
961+
self.root_hdlr)
962962
self.mem_logger = logging.getLogger('mem')
963963
self.mem_logger.propagate = 0
964964
self.mem_logger.addHandler(self.mem_hdlr)
@@ -995,6 +995,36 @@ def test_flush(self):
995995
self.mem_logger.debug(self.next_message())
996996
self.assert_log_lines(lines)
997997

998+
def test_flush_on_close(self):
999+
"""
1000+
Test that the flush-on-close configuration works as expected.
1001+
"""
1002+
self.mem_logger.debug(self.next_message())
1003+
self.assert_log_lines([])
1004+
self.mem_logger.info(self.next_message())
1005+
self.assert_log_lines([])
1006+
self.mem_logger.removeHandler(self.mem_hdlr)
1007+
# Default behaviour is to flush on close. Check that it happens.
1008+
self.mem_hdlr.close()
1009+
lines = [
1010+
('DEBUG', '1'),
1011+
('INFO', '2'),
1012+
]
1013+
self.assert_log_lines(lines)
1014+
# Now configure for flushing not to be done on close.
1015+
self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
1016+
self.root_hdlr,
1017+
False)
1018+
self.mem_logger.addHandler(self.mem_hdlr)
1019+
self.mem_logger.debug(self.next_message())
1020+
self.assert_log_lines(lines) # no change
1021+
self.mem_logger.info(self.next_message())
1022+
self.assert_log_lines(lines) # no change
1023+
self.mem_logger.removeHandler(self.mem_hdlr)
1024+
self.mem_hdlr.close()
1025+
# assert that no new lines have been added
1026+
self.assert_log_lines(lines) # no change
1027+
9981028

9991029
class ExceptionFormatter(logging.Formatter):
10001030
"""A special exception formatter."""

0 commit comments

Comments
 (0)