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.",
)
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 @@ 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_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

View File

@ -1178,11 +1178,10 @@ def test_disable_loggers(testdir):
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_disabling_works_with_log_cli(testdir):
)
result = testdir.runpytest(
"--log-cli-level=DEBUG",
"--logger-disable=disabled",
"--log-disable=disabled",
)
assert result.ret == ExitCode.OK
result.stdout.fnmatch_lines(