Apply proposed changes by niccodemus

This commit is contained in:
Itxaso Aizpurua 2022-10-23 17:45:22 +02:00
parent 456c8cfead
commit a0012ede97
2 changed files with 11 additions and 20 deletions

View File

@ -298,11 +298,11 @@ def pytest_addoption(parser: Parser) -> None:
help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.", help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.",
) )
group.addoption( group.addoption(
"--logger-disable", "--log-disable",
action="append", action="append",
default=[], default=[],
dest="logger_disable", dest="logger_disable",
help="Disable loggers by name", help="Disable a logger by name. Can be passed multipe times.",
) )
@ -601,14 +601,13 @@ class LoggingPlugin:
get_option_ini(config, "log_auto_indent"), get_option_ini(config, "log_auto_indent"),
) )
self.log_cli_handler.setFormatter(log_cli_formatter) self.log_cli_handler.setFormatter(log_cli_formatter)
self._disable_logger(loggers_to_disable=config.option.logger_disable) self._disable_loggers(loggers_to_disable=config.option.logger_disable)
def _disable_logger(self, loggers_to_disable: List[str]) -> None: def _disable_loggers(self, loggers_to_disable: List[str]) -> None:
if not loggers_to_disable: if not loggers_to_disable:
return return
logger_names_to_suppress = set(loggers_to_disable) for name in loggers_to_disable:
for name in logger_names_to_suppress:
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.disabled = True logger.disabled = True

View File

@ -1178,11 +1178,10 @@ def test_disable_loggers(testdir):
with caplog.at_level(logging.DEBUG): with caplog.at_level(logging.DEBUG):
disabled_log.warning("no log; no stderr") disabled_log.warning("no log; no stderr")
test_log.debug("Visible text!") test_log.debug("Visible text!")
print(os.linesep)
assert caplog.record_tuples == [('test', 10, 'Visible text!')] assert caplog.record_tuples == [('test', 10, 'Visible text!')]
""" """
) )
result = testdir.runpytest("--logger-disable=disabled", "-s") result = testdir.runpytest("--log-disable=disabled", "-s")
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
assert not result.stderr.lines assert not result.stderr.lines
@ -1198,24 +1197,17 @@ def test_disable_loggers_does_not_propagate(testdir):
def test_logger_propagation_to_parent(caplog): def test_logger_propagation_to_parent(caplog):
with caplog.at_level(logging.DEBUG): with caplog.at_level(logging.DEBUG):
child_logger.warning("some message") parent_logger.warning("some parent logger message")
print(os.linesep) child_logger.warning("some child logger message")
assert not caplog.record_tuples assert len(caplog.record_tuples) == 1
""" """
) )
result = testdir.runpytest("--logger-disable=parent.child", "-s") result = testdir.runpytest("--log-disable=parent.child", "-s")
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
assert not result.stderr.lines assert not result.stderr.lines
def test_disabled_loggers_help(testdir):
result = testdir.runpytest("-h")
result.stdout.fnmatch_lines(
[" --logger-disable=LOGGER_DISABLE", "*Disable loggers by name*"]
)
def test_log_disabling_works_with_log_cli(testdir): def test_log_disabling_works_with_log_cli(testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
@ -1230,7 +1222,7 @@ def test_log_disabling_works_with_log_cli(testdir):
) )
result = testdir.runpytest( result = testdir.runpytest(
"--log-cli-level=DEBUG", "--log-cli-level=DEBUG",
"--logger-disable=disabled", "--log-disable=disabled",
) )
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(