diff --git a/pymodbus/logging.py b/pymodbus/logging.py index aee5e19be..03893e1a4 100644 --- a/pymodbus/logging.py +++ b/pymodbus/logging.py @@ -39,6 +39,8 @@ class Log: """ _logger = logging.getLogger(__name__) + last_log_text = "" + repeat_log = False @classmethod def apply_logging_config(cls, level, log_file_name): @@ -88,33 +90,45 @@ def build_msg(cls, txt, *args): skip = True else: string_args.append(args[i]) - return txt.format(*string_args) + if (log_text := txt.format(*string_args)) != cls.last_log_text: + cls.last_log_text = log_text + cls.repeat_log = False + return log_text + if not cls.repeat_log: + cls.repeat_log = True + return "Repeating...." + return None @classmethod def info(cls, txt, *args): """Log info messages.""" if cls._logger.isEnabledFor(logging.INFO): - cls._logger.info(cls.build_msg(txt, *args), stacklevel=2) + if (log_text := cls.build_msg(txt, *args)): + cls._logger.info(log_text, stacklevel=2) @classmethod def debug(cls, txt, *args): """Log debug messages.""" if cls._logger.isEnabledFor(logging.DEBUG): - cls._logger.debug(cls.build_msg(txt, *args), stacklevel=2) + if (log_text := cls.build_msg(txt, *args)): + cls._logger.debug(log_text, stacklevel=2) @classmethod def warning(cls, txt, *args): """Log warning messages.""" if cls._logger.isEnabledFor(logging.WARNING): - cls._logger.warning(cls.build_msg(txt, *args), stacklevel=2) + if (log_text := cls.build_msg(txt, *args)): + cls._logger.warning(log_text, stacklevel=2) @classmethod def error(cls, txt, *args): """Log error messages.""" if cls._logger.isEnabledFor(logging.ERROR): - cls._logger.error(cls.build_msg(txt, *args), stacklevel=2) + if (log_text := cls.build_msg(txt, *args)): + cls._logger.error(log_text, stacklevel=2) @classmethod def critical(cls, txt, *args): """Log critical messages.""" - cls._logger.critical(cls.build_msg(txt, *args), stacklevel=2) + if (log_text := cls.build_msg(txt, *args)): + cls._logger.critical(log_text, stacklevel=2) diff --git a/test/not_updated/test_logging.py b/test/not_updated/test_logging.py index 1de923fbc..d118a06e2 100644 --- a/test/not_updated/test_logging.py +++ b/test/not_updated/test_logging.py @@ -43,22 +43,35 @@ def test_log_parms(self, txt, result, params): def test_apply_logging(self): """Test pymodbus_apply_logging_config.""" - pymodbus_apply_logging_config("debug") - pymodbus_apply_logging_config(logging.NOTSET) pymodbus_apply_logging_config("debug", "pymodbus.log") pymodbus_apply_logging_config("info") - Log.info("test") pymodbus_apply_logging_config(logging.NOTSET) - Log.warning("test") + Log.debug("test 1no") + pymodbus_apply_logging_config("debug") + Log.debug("test 1") + Log.debug("test 1") + Log.debug("test 1") + pymodbus_apply_logging_config(logging.NOTSET) + Log.warning("test 2no") pymodbus_apply_logging_config("warning") - Log.warning("test") + Log.warning("test 2") + Log.warning("test 2") + Log.warning("test 2") pymodbus_apply_logging_config(logging.NOTSET) - Log.critical("test") + Log.critical("test 3no") pymodbus_apply_logging_config("critical") - Log.critical("test") + Log.critical("test 3") + Log.critical("test 3") + Log.critical("test 3") pymodbus_apply_logging_config(logging.NOTSET) - Log.error("test") + Log.error("test 4no") pymodbus_apply_logging_config("error") - Log.error("test") + Log.error("test 4") + Log.error("test 4") + Log.error("test 4") pymodbus_apply_logging_config(logging.NOTSET) - Log.critical("test") + Log.info("test 5no") + pymodbus_apply_logging_config("info") + Log.info("test 5") + Log.info("test 5") + Log.info("test 5")