Merge pull request #7673 from bluetech/logging-fix-handler-restore

logging: fix handler level restored incorrectly if caplog.set_level is called more than once
This commit is contained in:
Ran Benita 2020-08-23 12:06:09 +03:00 committed by GitHub
commit 143e3ab846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 1 deletions

View File

@ -0,0 +1 @@
Fixed log-capturing level restored incorrectly if ``caplog.set_level`` is called more than once.

View File

@ -439,7 +439,8 @@ class LogCaptureFixture:
# Save the original log-level to restore it during teardown.
self._initial_logger_levels.setdefault(logger, logger_obj.level)
logger_obj.setLevel(level)
self._initial_handler_level = self.handler.level
if self._initial_handler_level is None:
self._initial_handler_level = self.handler.level
self.handler.setLevel(level)
@contextmanager

View File

@ -65,6 +65,7 @@ def test_change_level_undos_handler_level(testdir: Testdir) -> None:
def test1(caplog):
assert caplog.handler.level == 0
caplog.set_level(9999)
caplog.set_level(41)
assert caplog.handler.level == 41