diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index f9091399f..79fce6dd8 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -297,6 +297,13 @@ def pytest_addoption(parser: Parser) -> None: default=None, help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.", ) + group.addoption( + "--suppress-logger", + action="append", + default=[], + dest="suppress_logger", + help="Suppress logger by name", + ) _HandlerType = TypeVar("_HandlerType", bound=logging.Handler) @@ -595,6 +602,16 @@ class LoggingPlugin: ) self.log_cli_handler.setFormatter(log_cli_formatter) + if config.option.suppress_logger: + self._suppress_loggers() + + def _suppress_loggers(self) -> None: + logger_names_to_suppress = set(self._config.option.suppress_logger) + for name in logger_names_to_suppress: + logger = logging.getLogger(name) + logger.addFilter(lambda _: 0) + + def _create_formatter(self, log_format, log_date_format, auto_indent): # Color option doesn't exist if terminal plugin is disabled. color = getattr(self._config.option, "color", "no") diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 323ff7b24..e6f5d21fb 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1165,3 +1165,60 @@ def test_log_file_cli_subdirectories_are_successfully_created( result = pytester.runpytest("--log-file=foo/bar/logf.log") assert "logf.log" in os.listdir(expected) assert result.ret == ExitCode.OK + + +def test_suppress_loggers(testdir): + testdir.makepyfile( + """ + import logging + import os + suppressed_log = logging.getLogger('suppressed') + other_log = logging.getLogger('other') + normal_log = logging.getLogger('normal') + def test_logger_propagation(caplog): + with caplog.at_level(logging.DEBUG): + suppressed_log.warning("no log; no stderr") + other_log.info("no log") + normal_log.debug("Unsuppressed!") + print(os.linesep) + assert caplog.record_tuples == [('normal', 10, 'Unsuppressed!')] + """ + ) + result = testdir.runpytest( + "--suppress-logger=suppressed", "--suppress-logger=other", "-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): + testdir.makepyfile( + """ + import logging + log = logging.getLogger('mylog') + suppressed_log = logging.getLogger('suppressed') + def test_log_cli_works(caplog): + log.info("hello world") + suppressed_log.warning("Hello World") + """ + ) + result = testdir.runpytest( + "--log-cli-level=DEBUG", + "--suppress-logger=suppressed", + "--suppress-logger=mylog", + ) + assert result.ret == ExitCode.OK + result.stdout.no_fnmatch_line( + "INFO mylog:test_log_suppressing_works_with_log_cli.py:5 hello world" + ) + result.stdout.no_fnmatch_line( + "WARNING suppressed:test_log_suppressing_works_with_log_cli.py:6 Hello World" + ) + assert not result.stderr.lines