mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
gh-95804: Respect MemoryHandler.flushOnClose in logging shutdown. (GH-95857)
This commit is contained in:
parent
71c3d649b5
commit
37c0f9ccc0
4 changed files with 37 additions and 1 deletions
|
@ -2245,7 +2245,11 @@ def shutdown(handlerList=_handlerList):
|
||||||
if h:
|
if h:
|
||||||
try:
|
try:
|
||||||
h.acquire()
|
h.acquire()
|
||||||
h.flush()
|
# MemoryHandlers might not want to be flushed on close,
|
||||||
|
# but circular imports prevent us scoping this to just
|
||||||
|
# those handlers. hence the default to True.
|
||||||
|
if getattr(h, 'flushOnClose', True):
|
||||||
|
h.flush()
|
||||||
h.close()
|
h.close()
|
||||||
except (OSError, ValueError):
|
except (OSError, ValueError):
|
||||||
# Ignore errors which might be caused
|
# Ignore errors which might be caused
|
||||||
|
|
|
@ -1225,6 +1225,35 @@ class MemoryHandlerTest(BaseTest):
|
||||||
# assert that no new lines have been added
|
# assert that no new lines have been added
|
||||||
self.assert_log_lines(lines) # no change
|
self.assert_log_lines(lines) # no change
|
||||||
|
|
||||||
|
def test_shutdown_flush_on_close(self):
|
||||||
|
"""
|
||||||
|
Test that the flush-on-close configuration is respected by the
|
||||||
|
shutdown method.
|
||||||
|
"""
|
||||||
|
self.mem_logger.debug(self.next_message())
|
||||||
|
self.assert_log_lines([])
|
||||||
|
self.mem_logger.info(self.next_message())
|
||||||
|
self.assert_log_lines([])
|
||||||
|
# Default behaviour is to flush on close. Check that it happens.
|
||||||
|
logging.shutdown(handlerList=[logging.weakref.ref(self.mem_hdlr)])
|
||||||
|
lines = [
|
||||||
|
('DEBUG', '1'),
|
||||||
|
('INFO', '2'),
|
||||||
|
]
|
||||||
|
self.assert_log_lines(lines)
|
||||||
|
# Now configure for flushing not to be done on close.
|
||||||
|
self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
|
||||||
|
self.root_hdlr,
|
||||||
|
False)
|
||||||
|
self.mem_logger.addHandler(self.mem_hdlr)
|
||||||
|
self.mem_logger.debug(self.next_message())
|
||||||
|
self.assert_log_lines(lines) # no change
|
||||||
|
self.mem_logger.info(self.next_message())
|
||||||
|
self.assert_log_lines(lines) # no change
|
||||||
|
# assert that no new lines have been added after shutdown
|
||||||
|
logging.shutdown(handlerList=[logging.weakref.ref(self.mem_hdlr)])
|
||||||
|
self.assert_log_lines(lines) # no change
|
||||||
|
|
||||||
@threading_helper.requires_working_threading()
|
@threading_helper.requires_working_threading()
|
||||||
def test_race_between_set_target_and_flush(self):
|
def test_race_between_set_target_and_flush(self):
|
||||||
class MockRaceConditionHandler:
|
class MockRaceConditionHandler:
|
||||||
|
|
|
@ -198,6 +198,7 @@ Gawain Bolton
|
||||||
Carl Friedrich Bolz-Tereick
|
Carl Friedrich Bolz-Tereick
|
||||||
Forest Bond
|
Forest Bond
|
||||||
Gregory Bond
|
Gregory Bond
|
||||||
|
David Bonner
|
||||||
Angelin Booz
|
Angelin Booz
|
||||||
Médéric Boquien
|
Médéric Boquien
|
||||||
Matias Bordese
|
Matias Bordese
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix ``logging`` shutdown handler so it respects
|
||||||
|
``MemoryHandler.flushOnClose``.
|
Loading…
Add table
Add a link
Reference in a new issue