Repository navigation
gh-160936: Make logging setLevel() and disable() cost O(cached loggers) #160937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ar-maan05
wants to merge
4
commits into
python:main
Choose a base branch
from
Ar-maan05:logging-setlevel-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d60e39c
gh-160936: Make logging setLevel() and disable() cost O(cached loggers)
Ar-maan05 b586b0b
gh-160936: Clear all cached loggers unconditionally in _clear_cache
Ar-maan05 593b356
gh-160936: Check cache state before and after in the new logging tests
Ar-maan05 1edc2b1
gh-160936: Clear the logger cache after the new logging tests
Ar-maan05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6398,6 +6398,38 @@ def test_pickling(self): | |
| unpickled = pickle.loads(s) | ||
| self.assertIs(unpickled, logger) | ||
|
|
||
| def test_caching_unregistered_logger(self): | ||
| # A logger that is not in loggerDict must not keep a stale cache. | ||
| self.addCleanup(logging.root.manager._clear_cache) | ||
| logger = logging.Logger("unregistered") | ||
| self.assertEqual(logger._cache, {}) | ||
| logger.setLevel(logging.DEBUG) | ||
| self.assertEqual(logger._cache, {}) | ||
| self.assertTrue(logger.isEnabledFor(logging.DEBUG)) | ||
| self.assertEqual(logger._cache, {logging.DEBUG: True}) | ||
| logger.setLevel(logging.ERROR) | ||
| self.assertEqual(logger._cache, {}) | ||
| self.assertFalse(logger.isEnabledFor(logging.DEBUG)) | ||
| self.assertEqual(logger._cache, {logging.DEBUG: False}) | ||
|
|
||
| def test_clear_cache_empties_cached_loggers(self): | ||
| manager = logging.root.manager | ||
| self.addCleanup(manager._clear_cache) | ||
| registered = logging.getLogger("cached.registered") | ||
| unregistered = logging.Logger("cached.unregistered") | ||
| # Start from a known state: other tests may have cached results. | ||
| manager._clear_cache() | ||
|
Comment on lines
+6420
to
+6421
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shoudl also have some cleanup (or if it's not the case, you can add this in setUp() and tearDown()). We don't want the cache to be possibly polluted for the next test. |
||
| self.assertEqual(len(manager._cached_loggers), 0) | ||
| for logger in (registered, unregistered): | ||
| logger.isEnabledFor(logging.DEBUG) | ||
| self.assertIn(logger, manager._cached_loggers) | ||
|
Ar-maan05 marked this conversation as resolved.
Resolved
|
||
| self.assertEqual(len(manager._cached_loggers), 2) | ||
| registered.setLevel(logging.INFO) | ||
| self.assertEqual(len(manager._cached_loggers), 0) | ||
| # A later cache miss starts tracking the logger again. | ||
| registered.isEnabledFor(logging.DEBUG) | ||
| self.assertEqual(list(manager._cached_loggers), [registered]) | ||
|
|
||
| def test_caching(self): | ||
| root = self.root_logger | ||
| logger1 = logging.getLogger("abc") | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2026-10-29-20-30-00.gh-issue-160936.lgCach.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Speed up :meth:`logging.Logger.setLevel` and :func:`logging.disable` when | ||
| there are many loggers: only loggers that have cached results are now cleared, | ||
| instead of every logger. This also clears the cache of loggers that are not | ||
| registered with the manager, which could previously keep a stale result. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.