Skip to content

Commit de7deff

Browse files
committed
improve logging config: don't modify root logger
1 parent 78dd670 commit de7deff

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: pytest_mpl/plugin.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,22 @@ def __init__(self,
298298
self._test_stats = None
299299
self.return_value = {}
300300

301-
# https://stackoverflow.com/questions/51737378/how-should-i-log-in-my-pytest-plugin
302-
# turn debug prints on only if "-vv" or more passed
301+
# configure a separate logger for this pluggin which is independent
302+
# of the options that are configured for pytest or for the code that
303+
# is tested; turn debug prints on only if "-vv" or more passed
303304
level = logging.DEBUG if config.option.verbose > 1 else logging.INFO
304-
logging.basicConfig(level=level)
305+
if config.option.log_cli_format is not None:
306+
fmt = config.option.log_cli_format
307+
else:
308+
# use pytest's default fmt
309+
fmt = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s"
310+
formatter = logging.Formatter(fmt)
311+
handler = logging.StreamHandler()
312+
handler.setFormatter(formatter)
305313
self.logger = logging.getLogger('pytest-mpl')
314+
self.logger.propagate = False
315+
self.logger.setLevel(level)
316+
self.logger.addHandler(handler)
306317

307318
def generate_filename(self, item):
308319
"""

0 commit comments

Comments
 (0)