From 3511bf44f53caba7cfa2bc573987a9e4a457c47a Mon Sep 17 00:00:00 2001 From: Itxaso Aizpurua Date: Mon, 17 Oct 2022 20:47:53 +0200 Subject: [PATCH] change name to --logger-disabled, refactor and testing for parent propagation --- src/_pytest/logging.py | 15 +++++---- testing/logging/test_reporting.py | 56 +++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index b08b3bc70..d523d26e4 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -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.", ) group.addoption( - "--suppress-logger", + "--logger-disabled", action="append", default=[], - dest="suppress_logger", - help="Suppress loggers by name", + dest="logger_disabled", + help="Disable loggers by name", ) @@ -601,12 +601,13 @@ class LoggingPlugin: get_option_ini(config, "log_auto_indent"), ) self.log_cli_handler.setFormatter(log_cli_formatter) + self._disable_logger(loggers_to_disable=config.option.logger_disabled) - if config.option.suppress_logger: - self._suppress_loggers() + def _disable_logger(self, loggers_to_disable: List[str]) -> None: + if not loggers_to_disable: + return - def _suppress_loggers(self) -> None: - logger_names_to_suppress = set(self._config.option.suppress_logger) + logger_names_to_suppress = set(loggers_to_disable) for name in logger_names_to_suppress: logger = logging.getLogger(name) logger.addFilter(lambda _: 0) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index f4ae5130a..7d765b3f9 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1167,54 +1167,76 @@ def test_log_file_cli_subdirectories_are_successfully_created( assert result.ret == ExitCode.OK -def test_suppress_loggers(testdir): +def test_disable_loggers(testdir): testdir.makepyfile( """ import logging import os - suppressed_log = logging.getLogger('suppressed') + disabled_log = logging.getLogger('disabled') test_log = logging.getLogger('test') def test_logger_propagation(caplog): with caplog.at_level(logging.DEBUG): - suppressed_log.warning("no log; no stderr") + 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("--suppress-logger=suppressed", "-s") + result = testdir.runpytest("--logger-disabled=disabled", "-s") assert result.ret == ExitCode.OK assert not result.stderr.lines -def test_suppress_loggers_help(testdir): - result = testdir.runpytest("-h") - result.stdout.fnmatch_lines( - [" --suppress-logger=SUPPRESS_LOGGER", "*Suppress loggers by name*"] - ) - - -def test_log_suppressing_works_with_log_cli(testdir): +def test_disable_loggers_does_not_propagate(testdir): testdir.makepyfile( """ import logging - suppressed_log = logging.getLogger('suppressed') + import os + + parent_logger = logging.getLogger("parent") + child_logger = parent_logger.getChild("child") + + 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 + """ + ) + + result = testdir.runpytest("--logger-disabled=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-disabled=LOGGER_DISABLED", "*Disable loggers by name*"] + ) + + +def test_log_disabling_works_with_log_cli(testdir): + testdir.makepyfile( + """ + import logging + disabled_log = logging.getLogger('disabled') test_log = logging.getLogger('test') def test_log_cli_works(caplog): test_log.info("Visible text!") - suppressed_log.warning("This string will be suppressed.") + disabled_log.warning("This string will be suppressed.") """ ) result = testdir.runpytest( "--log-cli-level=DEBUG", - "--suppress-logger=suppressed", + "--logger-disabled=disabled", ) assert result.ret == ExitCode.OK result.stdout.fnmatch_lines( - "INFO test:test_log_suppressing_works_with_log_cli.py:6 Visible text!" + "INFO test:test_log_disabling_works_with_log_cli.py:6 Visible text!" ) result.stdout.no_fnmatch_line( - "WARNING suppressed:test_log_suppressing_works_with_log_cli.py:7 This string will be suppressed." + "WARNING disabled:test_log_disabling_works_with_log_cli.py:7 This string will be suppressed." ) assert not result.stderr.lines