From de7deff1579abd6b09e20a86d14c2ae70c864812 Mon Sep 17 00:00:00 2001 From: theOehrly Date: Sun, 23 Oct 2022 19:34:38 +0200 Subject: [PATCH] improve logging config: don't modify root logger --- pytest_mpl/plugin.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 3ddc24b4..13cced37 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -298,11 +298,22 @@ def __init__(self, self._test_stats = None self.return_value = {} - # https://stackoverflow.com/questions/51737378/how-should-i-log-in-my-pytest-plugin - # turn debug prints on only if "-vv" or more passed + # configure a separate logger for this pluggin which is independent + # of the options that are configured for pytest or for the code that + # is tested; turn debug prints on only if "-vv" or more passed level = logging.DEBUG if config.option.verbose > 1 else logging.INFO - logging.basicConfig(level=level) + if config.option.log_cli_format is not None: + fmt = config.option.log_cli_format + else: + # use pytest's default fmt + fmt = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s" + formatter = logging.Formatter(fmt) + handler = logging.StreamHandler() + handler.setFormatter(formatter) self.logger = logging.getLogger('pytest-mpl') + self.logger.propagate = False + self.logger.setLevel(level) + self.logger.addHandler(handler) def generate_filename(self, item): """