فهرست منبع

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 سال پیش
والد
کامیت
b14cff5497
1فایلهای تغییر یافته به همراه7 افزوده شده و 5 حذف شده
  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)