summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Bonner <dbonner@gmail.com>2022-08-10 17:08:55 (GMT)
committerGitHub <noreply@github.com>2022-08-10 17:08:55 (GMT)
commit37c0f9ccc06750a7e22f5c176df39373f7aca526 (patch)
tree8d86775d55fb448b8e12f608430a50bb879a0b57
parent71c3d649b5a0324c6eb01f9ad025c1e102b82bba (diff)
downloadcpython-37c0f9ccc06750a7e22f5c176df39373f7aca526.zip
cpython-37c0f9ccc06750a7e22f5c176df39373f7aca526.tar.gz
cpython-37c0f9ccc06750a7e22f5c176df39373f7aca526.tar.bz2
gh-95804: Respect MemoryHandler.flushOnClose in logging shutdown. (GH-95857)
-rw-r--r--Lib/logging/__init__.py6
-rw-r--r--Lib/test/test_logging.py29
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2022-08-10-11-54-04.gh-issue-95804.i5FCFK.rst2
4 files changed, 37 insertions, 1 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index dc28702..afb5234 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -2245,7 +2245,11 @@ def shutdown(handlerList=_handlerList):
if h:
try:
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()
except (OSError, ValueError):
# Ignore errors which might be caused
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index a505e80..99ea2f6 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1225,6 +1225,35 @@ class MemoryHandlerTest(BaseTest):
# assert that no new lines have been added
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()
def test_race_between_set_target_and_flush(self):
class MockRaceConditionHandler:
diff --git a/Misc/ACKS b/Misc/ACKS
index 28b4ce4..c1f570a 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -198,6 +198,7 @@ Gawain Bolton
Carl Friedrich Bolz-Tereick
Forest Bond
Gregory Bond
+David Bonner
Angelin Booz
Médéric Boquien
Matias Bordese
diff --git a/Misc/NEWS.d/next/Library/2022-08-10-11-54-04.gh-issue-95804.i5FCFK.rst b/Misc/NEWS.d/next/Library/2022-08-10-11-54-04.gh-issue-95804.i5FCFK.rst
new file mode 100644
index 0000000..46434cb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-10-11-54-04.gh-issue-95804.i5FCFK.rst
@@ -0,0 +1,2 @@
+Fix ``logging`` shutdown handler so it respects
+``MemoryHandler.flushOnClose``.