Fix output duplication from I2nLogger
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 24 Jun 2025 08:07:08 +0000 (10:07 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 24 Jun 2025 08:13:40 +0000 (10:13 +0200)
Finally found the reason for duplicate logging lines appearing sometimes
when using the log_helpers.I2nLogger: subsequent import of python's
logging module and creation of loggers from there probably lead to
re-initialization of the root logger. Do not know a simple solution to
this, so disable propagation to the root logger for now.

src/log_helpers.py

index 9f12bc6..67b91a3 100644 (file)
@@ -191,6 +191,11 @@ class I2nLogger:
 
     ..note:: Do not change or use the underlying logger or functionality here
              (in particular, get_level) is no longer reliable!
+
+    ..warn:: Propagation to root logger has to be disabled in these loggers
+             since otherwise any other use of python's :py:mod:`logging`
+             module leads to re-initialization of the root logger with handler
+             to stderr and thus duplicate logging output
     """
 
     def __init__(self, name, level=INFO, fmt=DEFAULT_SHORT_LEVEL_FORMAT,
@@ -227,6 +232,10 @@ class I2nLogger:
                              .format(name))
 
         self._log = logging.getLogger(name)
+        if '.' not in name:
+            # root logger may be re-initialized from other imported modules,
+            # so propagation to it will lead to duplicate output
+            self._log.propagate = False
         if isinstance(level, str):
             level = LEVEL_DICT[level]
         self._level = min(MAX_LEVEL, max(MIN_LEVEL, level))