From d8fc4f148c19d245115b6cda8ced45c88a4f6423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Wed, 15 Jan 2025 15:41:04 +0100 Subject: [PATCH] Respect existing handlers Signed-off-by: Carlos Mocholi --- flogging/flogging.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/flogging/flogging.py b/flogging/flogging.py index d263bf9..605bc30 100644 --- a/flogging/flogging.py +++ b/flogging/flogging.py @@ -273,21 +273,22 @@ def ensure_utf8_stream(stream): sys.stdout, sys.stderr = (ensure_utf8_stream(s) for s in (sys.stdout, sys.stderr)) np_set_string_function(repr_array) - # basicConfig is only called to make sure there is at least one handler for the root logger. - # All the output level setting is down right afterwards. - logging.basicConfig() + root = logging.getLogger() + if not root.handlers: + # basicConfig is only called to make sure there is at least one handler for the root logger. + # All the output level setting is down right afterwards. + logging.basicConfig() logging.captureWarnings(capture=True) for key, val in os.environ.items(): if key.startswith("flog_"): domain = key[len("flog_") :] logging.getLogger(domain).setLevel(logging._nameToLevel.get(val, level)) - root = logging.getLogger() root.setLevel(level) if not structured: handler = root.handlers[0] # pytest injects DontReadFromInput which does not have "closed" - if not getattr(sys.stdin, "closed", False) and sys.stdout.isatty(): + if handler.formatter is None and not getattr(sys.stdin, "closed", False) and sys.stdout.isatty(): handler.setFormatter(AwesomeFormatter()) else: handler = StructuredHandler(level, level_from_msg)