logging: fix handler level restored incorrectly if caplog.set_level is called more than once

This commit is contained in:
Ran Benita 2020-08-22 17:23:26 +03:00
parent d69abff2c7
commit b1354608cc
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,6 +439,7 @@ class LogCaptureFixture:
# Save the original log-level to restore it during teardown. # Save the original log-level to restore it during teardown.
self._initial_logger_levels.setdefault(logger, logger_obj.level) self._initial_logger_levels.setdefault(logger, logger_obj.level)
logger_obj.setLevel(level) logger_obj.setLevel(level)
if self._initial_handler_level is None:
self._initial_handler_level = self.handler.level self._initial_handler_level = self.handler.level
self.handler.setLevel(level) self.handler.setLevel(level)

View File

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