Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a --suppress-logger option to logging to suppress logging for one or multiple loggers #7873

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7431.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``--suppress-logger`` CLI option added to suppress output from individual loggers
18 changes: 18 additions & 0 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
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 loggers by name",
)


_HandlerType = TypeVar("_HandlerType", bound=logging.Handler)
Expand Down Expand Up @@ -575,6 +582,17 @@ def __init__(self, config: Config) -> None:
get_option_ini(config, "log_auto_indent"),
)
self.log_cli_handler.setFormatter(log_cli_formatter)
if config.option.suppress_logger:
self._suppress_loggers()

def _suppress_loggers(self) -> None:
logger_names = set(self._config.option.suppress_logger)
for name in logger_names:
# disable propagation for the given logger preventing of event passing to ancestor handlers
# adding a NullHandler to prevent logging.LastResort handler from logging warnings to stderr
logger = logging.getLogger(name)
logger.addHandler(logging.NullHandler())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure about this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure myself, but what happens to other handlers that are installed in the logger? I suppose they will still process messages?

Copy link
Member Author

@symonk symonk Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting one, from a quick check it will still use such handlers, this is outlined below:

>>> import logging
>>> log = logging.getLogger('pytest')
>>> log.setLevel(logging.DEBUG)
>>> # add a stream handler to stderr by default
>>> log.addHandler(logging.StreamHandler())
>>> log.debug('stderr')
stderr
>>> log.addHandler(logging.NullHandler())
>>> log.debug('does this still write to stderr')
does this still write to stderr

two questions:

  • Should we iterate / remove the handlers on the given logger?
  • Should we check the hierarchy of loggers if the logger provided exists, else I assume its going to register a new one by badly typed --suppress-logger names into the hierarchy dict (maybe thats not a big deal)

Copy link
Member Author

@symonk symonk Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1 check name is in: 
logs = [logging.getLogger(name).name for name in logging.root.manager.loggerDict] ?

#2 empty logger handlers for the given logger, unsure of the implications here
logger.handlers = [logging.NullHandler()]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we do to suppress logs of certain loggers is to simply set the logLevel of those loggers to CRITICAL. Have you thought about doing that?

Copy link
Member Author

@symonk symonk Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I didn’t think about that as I was thinking about fully suppressing any level, however doing something like that may be better suited as I’d assume even users who wanted this feature would/should actually care about such messages from those libraries etc. Feels like a nice middle ground, thoughts?

logger.propagate = False

def _create_formatter(self, log_format, log_date_format, auto_indent):
# Color option doesn't exist if terminal plugin is disabled.
Expand Down
57 changes: 57 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,3 +1152,60 @@ def test_log_file_cli_subdirectories_are_successfully_created(testdir):
result = testdir.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