Skip to content

Commit c1a3122

Browse files
esadomereendebakpt
andcommitted
gh-132372: Batch logging cache clearing
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
1 parent b7a1a9b commit c1a3122

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/logging/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,20 @@ def _handle_existing_loggers(existing, child_loggers, disable_existing):
188188
disabled if disable_existing is false.
189189
"""
190190
root = logging.root
191+
cache_clear_needed = False
191192
for log in existing:
192193
logger = root.manager.loggerDict[log]
193194
if log in child_loggers:
194195
if not isinstance(logger, logging.PlaceHolder):
195-
logger.setLevel(logging.NOTSET)
196+
# Equivalent to setLevel(NOTSET), but clear the cache once.
197+
logger.level = logging.NOTSET
196198
logger.handlers = []
197199
logger.propagate = True
200+
cache_clear_needed = True
198201
else:
199202
logger.disabled = disable_existing
203+
if cache_clear_needed:
204+
root.manager._clear_cache()
200205

201206
def _discard_existing_logger(name, existing, existing_set, child_loggers):
202207
"""Discard a configured logger and record its existing children."""

Lib/test/test_logging.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4176,6 +4176,8 @@ def test_90195(self):
41764176
def test_disable_existing_loggers_preserves_children(self):
41774177
parent = logging.getLogger('many')
41784178
child = logging.getLogger('many.child')
4179+
child.setLevel(logging.CRITICAL)
4180+
self.assertFalse(child.isEnabledFor(logging.INFO))
41794181
cousin = logging.getLogger('many-child')
41804182
for i in range(20):
41814183
logging.getLogger(f'many-sibling-{i}')
@@ -4191,6 +4193,8 @@ def test_disable_existing_loggers_preserves_children(self):
41914193

41924194
self.assertFalse(parent.disabled)
41934195
self.assertFalse(child.disabled)
4196+
self.assertEqual(child.level, logging.NOTSET)
4197+
self.assertTrue(child.isEnabledFor(logging.INFO))
41944198
self.assertTrue(cousin.disabled)
41954199

41964200
def test_111615(self):

0 commit comments

Comments
 (0)