Kaynağa Gözat

Prevent setting the default logger multiple times

This would result in each log line being printed multiple times.

Signed-off-by: Stan Janssen <stan.janssen@elaad.nl>
Stan Janssen 4 yıl önce
ebeveyn
işleme
b14cff5497
1 değiştirilmiş dosya ile 7 ekleme ve 5 silme
  1. 7 5
      openleadr/__init__.py

+ 7 - 5
openleadr/__init__.py

@@ -30,8 +30,10 @@ def enable_default_logging(level=logging.INFO):
     import sys
     import logging
     logger = logging.getLogger('openleadr')
-    logger.setLevel(level)
-
-    logging_handler = logging.StreamHandler(stream=sys.stdout)
-    logging_handler.setLevel(logging.DEBUG)
-    logger.addHandler(logging_handler)
+    handler_names = [handler.name for handler in logger.handlers]
+    if 'openleadr_default_handler' not in handler_names:
+        logger.setLevel(level)
+        logging_handler = logging.StreamHandler(stream=sys.stdout)
+        logging_handler.set_name('openleadr_default_handler')
+        logging_handler.setLevel(logging.DEBUG)
+        logger.addHandler(logging_handler)