Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-132372: Batch logging cache clearing
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
  • Loading branch information
esadomer and eendebakpt committed May 27, 2026
commit 4040d60321b083c7645c357313a0c2be8f3fa561
7 changes: 6 additions & 1 deletion Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,20 @@ def _handle_existing_loggers(existing, child_loggers, disable_existing):
disabled if disable_existing is false.
"""
root = logging.root
cache_clear_needed = False
for log in existing:
logger = root.manager.loggerDict[log]
if log in child_loggers:
if not isinstance(logger, logging.PlaceHolder):
logger.setLevel(logging.NOTSET)
# Equivalent to setLevel(NOTSET), but clear the cache once.
logger.level = logging.NOTSET

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change really necessary? while setLevel would clear the cache multiple times, I would prefer that we pass through logger.setLevel public API instead of changing .level directly, just in case someone has a logger subclass that does something else with setLevel.

logger.handlers = []
logger.propagate = True
cache_clear_needed = True
else:
logger.disabled = disable_existing
if cache_clear_needed:
root.manager._clear_cache()

def _discard_existing_logger(name, existing, existing_set, child_loggers):

@picnixz picnixz May 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _discard_existing_logger(name, existing, existing_set, child_loggers):
def _forget_existing_logger(name, existing, existing_set, child_loggers):

Maybe something like that instead (since we also update child_loggers)

"""Discard a configured logger and record its existing children."""
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4176,6 +4176,8 @@ def test_90195(self):
def test_disable_existing_loggers_preserves_children(self):
parent = logging.getLogger('many')
child = logging.getLogger('many.child')
child.setLevel(logging.CRITICAL)
self.assertFalse(child.isEnabledFor(logging.INFO))
cousin = logging.getLogger('many-child')
for i in range(20):
logging.getLogger(f'many-sibling-{i}')
Expand All @@ -4191,6 +4193,8 @@ def test_disable_existing_loggers_preserves_children(self):

self.assertFalse(parent.disabled)
self.assertFalse(child.disabled)
self.assertEqual(child.level, logging.NOTSET)
self.assertTrue(child.isEnabledFor(logging.INFO))
self.assertTrue(cousin.disabled)

def test_111615(self):
Expand Down