change name to --logger-disabled, refactor and testing for parent propagation
This commit is contained in:
parent
efc35545dc
commit
3511bf44f5
|
@ -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.",
|
help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.",
|
||||||
)
|
)
|
||||||
group.addoption(
|
group.addoption(
|
||||||
"--suppress-logger",
|
"--logger-disabled",
|
||||||
action="append",
|
action="append",
|
||||||
default=[],
|
default=[],
|
||||||
dest="suppress_logger",
|
dest="logger_disabled",
|
||||||
help="Suppress loggers by name",
|
help="Disable loggers by name",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -601,12 +601,13 @@ class LoggingPlugin:
|
||||||
get_option_ini(config, "log_auto_indent"),
|
get_option_ini(config, "log_auto_indent"),
|
||||||
)
|
)
|
||||||
self.log_cli_handler.setFormatter(log_cli_formatter)
|
self.log_cli_handler.setFormatter(log_cli_formatter)
|
||||||
|
self._disable_logger(loggers_to_disable=config.option.logger_disabled)
|
||||||
|
|
||||||
if config.option.suppress_logger:
|
def _disable_logger(self, loggers_to_disable: List[str]) -> None:
|
||||||
self._suppress_loggers()
|
if not loggers_to_disable:
|
||||||
|
return
|
||||||
|
|
||||||
def _suppress_loggers(self) -> None:
|
logger_names_to_suppress = set(loggers_to_disable)
|
||||||
logger_names_to_suppress = set(self._config.option.suppress_logger)
|
|
||||||
for name in logger_names_to_suppress:
|
for name in logger_names_to_suppress:
|
||||||
logger = logging.getLogger(name)
|
logger = logging.getLogger(name)
|
||||||
logger.addFilter(lambda _: 0)
|
logger.addFilter(lambda _: 0)
|
||||||
|
|
|
@ -1167,54 +1167,76 @@ def test_log_file_cli_subdirectories_are_successfully_created(
|
||||||
assert result.ret == ExitCode.OK
|
assert result.ret == ExitCode.OK
|
||||||
|
|
||||||
|
|
||||||
def test_suppress_loggers(testdir):
|
def test_disable_loggers(testdir):
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
suppressed_log = logging.getLogger('suppressed')
|
disabled_log = logging.getLogger('disabled')
|
||||||
test_log = logging.getLogger('test')
|
test_log = logging.getLogger('test')
|
||||||
def test_logger_propagation(caplog):
|
def test_logger_propagation(caplog):
|
||||||
with caplog.at_level(logging.DEBUG):
|
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!")
|
test_log.debug("Visible text!")
|
||||||
print(os.linesep)
|
print(os.linesep)
|
||||||
assert caplog.record_tuples == [('test', 10, 'Visible text!')]
|
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 result.ret == ExitCode.OK
|
||||||
assert not result.stderr.lines
|
assert not result.stderr.lines
|
||||||
|
|
||||||
|
|
||||||
def test_suppress_loggers_help(testdir):
|
def test_disable_loggers_does_not_propagate(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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
import logging
|
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')
|
test_log = logging.getLogger('test')
|
||||||
|
|
||||||
def test_log_cli_works(caplog):
|
def test_log_cli_works(caplog):
|
||||||
test_log.info("Visible text!")
|
test_log.info("Visible text!")
|
||||||
suppressed_log.warning("This string will be suppressed.")
|
disabled_log.warning("This string will be suppressed.")
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
result = testdir.runpytest(
|
result = testdir.runpytest(
|
||||||
"--log-cli-level=DEBUG",
|
"--log-cli-level=DEBUG",
|
||||||
"--suppress-logger=suppressed",
|
"--logger-disabled=disabled",
|
||||||
)
|
)
|
||||||
assert result.ret == ExitCode.OK
|
assert result.ret == ExitCode.OK
|
||||||
result.stdout.fnmatch_lines(
|
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(
|
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
|
assert not result.stderr.lines
|
||||||
|
|
Loading…
Reference in New Issue