From d76b7978d6acef2b9b5f7956d4f39fc829d1858e Mon Sep 17 00:00:00 2001 From: Luc Edes Date: Fri, 14 Jul 2023 11:00:40 -0400 Subject: [PATCH] Avoid duplicate logging to stdout (#185) --- conflowgen/logging/logging.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/conflowgen/logging/logging.py b/conflowgen/logging/logging.py index eb25d9c9..531c084f 100644 --- a/conflowgen/logging/logging.py +++ b/conflowgen/logging/logging.py @@ -59,10 +59,15 @@ def setup_logger( logger = logging.getLogger("conflowgen") logger.setLevel(logging.DEBUG) - flow_handler = logging.StreamHandler(stream=sys.stdout) - flow_handler.setLevel(logging.DEBUG) - flow_handler.setFormatter(formatter) - logger.addHandler(flow_handler) + stream_handlers = [handler for handler in logger.handlers if isinstance(handler, logging.StreamHandler)] + if any(handler.stream == sys.stdout for handler in stream_handlers): + logger.warning("Duplicate StreamHandler streaming to sys.stdout detected. " + "Skipping adding another StreamHandler.") + else: + stream_handler = logging.StreamHandler(stream=sys.stdout) + stream_handler.setLevel(logging.DEBUG) + stream_handler.setFormatter(formatter) + logger.addHandler(stream_handler) if not os.path.isdir(logging_directory): logger.debug(f"Creating log directory at {logging_directory}")