Skip to content

Commit

Permalink
Avoid duplicate logging to stdout (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucedes27 authored Jul 14, 2023
1 parent 3079e08 commit d76b797
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions conflowgen/logging/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit d76b797

Please sign in to comment.