[New-bugs-announce] [issue37258] Logging cache not cleared properly when setting level

David Wang report at bugs.python.org
Wed Jun 12 17:48:28 EDT 2019


New submission from David Wang <dwang at roblox.com>:

If you call setLevel() on a subclass of logging.Logger, it does not reset the cache for that logger. This mean that if you make some logging calls, then call setLevel(), the logger still acts like it still has its old level. See the attached python file for a reference.

Currently, the user has to call logger._cache.clear() to manually clear the cache after calling setLevel(). To fix this in Python, we would have to change Logger.setLevel() in /logging/__init__.py to have the following code
```
self.level = _checkLevel(level)
self.manager._clear_cache()
self._cache.clear()
```

Note the following:
- I made sure the subclass has a handler attached so setLevel() should work
- This bug does not occur if you use logging.getLogger(). This is because logging.getLogger() returns the root logger, and the cache clear specifically targets the root logger's cache to be cleared. It occurs when the logger is specifically subclassed from logging.getLoggerClass()
- The cache was added in Python 3.7, so this bug is specific to this version of python.

----------
components: Library (Lib)
files: test.py
messages: 345414
nosy: David Wang
priority: normal
severity: normal
status: open
title: Logging cache not cleared properly when setting level
type: behavior
versions: Python 3.7
Added file: https://bugs.python.org/file48416/test.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37258>
_______________________________________


More information about the New-bugs-announce mailing list