From d8c57bf31409dfe6711b0a0c38ace6f0b635bcdd Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 24 Jun 2025 10:07:08 +0200 Subject: [PATCH] Fix output duplication from I2nLogger 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 | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/src/log_helpers.py b/src/log_helpers.py index 9f12bc6..67b91a3 100644 --- a/src/log_helpers.py +++ b/src/log_helpers.py @@ -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)) -- 1.7.1