Skip to content

Commit

Permalink
apply proposed changes - @nicoddemus
Browse files Browse the repository at this point in the history
  • Loading branch information
itxasos23 committed Oct 23, 2022
1 parent 456c8cf commit e9d8982
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)


Expand Down Expand Up @@ -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

Expand Down
20 changes: 6 additions & 14 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
"""
Expand All @@ -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(
Expand Down

0 comments on commit e9d8982

Please sign in to comment.