diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index d1713845fad..4e3d12475d1 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -298,11 +298,11 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs): help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.", ) group.addoption( - "--logger-disable", + "--log-disable", action="append", default=[], dest="logger_disable", - help="Disable loggers by name", + help="Disable a logger by name. Can be passed multipe times.", ) @@ -601,14 +601,13 @@ def __init__(self, config: Config) -> None: get_option_ini(config, "log_auto_indent"), ) 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: return - logger_names_to_suppress = set(loggers_to_disable) - for name in logger_names_to_suppress: + for name in loggers_to_disable: logger = logging.getLogger(name) logger.disabled = True diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 4985adc8417..a7de56d5cc2 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1178,11 +1178,10 @@ def test_logger_propagation(caplog): with caplog.at_level(logging.DEBUG): disabled_log.warning("no log; no stderr") test_log.debug("Visible text!") - print(os.linesep) 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 not result.stderr.lines @@ -1198,24 +1197,17 @@ def test_disable_loggers_does_not_propagate(testdir): def test_logger_propagation_to_parent(caplog): with caplog.at_level(logging.DEBUG): - child_logger.warning("some message") - print(os.linesep) - assert not caplog.record_tuples + parent_logger.warning("some parent logger message") + child_logger.warning("some child logger message") + 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 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): testdir.makepyfile( """ @@ -1230,7 +1222,7 @@ def test_log_cli_works(caplog): ) result = testdir.runpytest( "--log-cli-level=DEBUG", - "--logger-disable=disabled", + "--log-disable=disabled", ) assert result.ret == ExitCode.OK result.stdout.fnmatch_lines(