Skip to content

Commit 10e234b

Browse files
authored
Remove duplicate log lines. (#2736)
1 parent 8d2634f commit 10e234b

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

pymodbus/logging.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Log:
3939
"""
4040

4141
_logger = logging.getLogger(__name__)
42+
last_log_text = ""
43+
repeat_log = False
4244

4345
@classmethod
4446
def apply_logging_config(cls, level, log_file_name):
@@ -88,33 +90,45 @@ def build_msg(cls, txt, *args):
8890
skip = True
8991
else:
9092
string_args.append(args[i])
91-
return txt.format(*string_args)
93+
if (log_text := txt.format(*string_args)) != cls.last_log_text:
94+
cls.last_log_text = log_text
95+
cls.repeat_log = False
96+
return log_text
97+
if not cls.repeat_log:
98+
cls.repeat_log = True
99+
return "Repeating...."
100+
return None
92101

93102
@classmethod
94103
def info(cls, txt, *args):
95104
"""Log info messages."""
96105
if cls._logger.isEnabledFor(logging.INFO):
97-
cls._logger.info(cls.build_msg(txt, *args), stacklevel=2)
106+
if (log_text := cls.build_msg(txt, *args)):
107+
cls._logger.info(log_text, stacklevel=2)
98108

99109
@classmethod
100110
def debug(cls, txt, *args):
101111
"""Log debug messages."""
102112
if cls._logger.isEnabledFor(logging.DEBUG):
103-
cls._logger.debug(cls.build_msg(txt, *args), stacklevel=2)
113+
if (log_text := cls.build_msg(txt, *args)):
114+
cls._logger.debug(log_text, stacklevel=2)
104115

105116
@classmethod
106117
def warning(cls, txt, *args):
107118
"""Log warning messages."""
108119
if cls._logger.isEnabledFor(logging.WARNING):
109-
cls._logger.warning(cls.build_msg(txt, *args), stacklevel=2)
120+
if (log_text := cls.build_msg(txt, *args)):
121+
cls._logger.warning(log_text, stacklevel=2)
110122

111123
@classmethod
112124
def error(cls, txt, *args):
113125
"""Log error messages."""
114126
if cls._logger.isEnabledFor(logging.ERROR):
115-
cls._logger.error(cls.build_msg(txt, *args), stacklevel=2)
127+
if (log_text := cls.build_msg(txt, *args)):
128+
cls._logger.error(log_text, stacklevel=2)
116129

117130
@classmethod
118131
def critical(cls, txt, *args):
119132
"""Log critical messages."""
120-
cls._logger.critical(cls.build_msg(txt, *args), stacklevel=2)
133+
if (log_text := cls.build_msg(txt, *args)):
134+
cls._logger.critical(log_text, stacklevel=2)

test/not_updated/test_logging.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,35 @@ def test_log_parms(self, txt, result, params):
4343

4444
def test_apply_logging(self):
4545
"""Test pymodbus_apply_logging_config."""
46-
pymodbus_apply_logging_config("debug")
47-
pymodbus_apply_logging_config(logging.NOTSET)
4846
pymodbus_apply_logging_config("debug", "pymodbus.log")
4947
pymodbus_apply_logging_config("info")
50-
Log.info("test")
5148
pymodbus_apply_logging_config(logging.NOTSET)
52-
Log.warning("test")
49+
Log.debug("test 1no")
50+
pymodbus_apply_logging_config("debug")
51+
Log.debug("test 1")
52+
Log.debug("test 1")
53+
Log.debug("test 1")
54+
pymodbus_apply_logging_config(logging.NOTSET)
55+
Log.warning("test 2no")
5356
pymodbus_apply_logging_config("warning")
54-
Log.warning("test")
57+
Log.warning("test 2")
58+
Log.warning("test 2")
59+
Log.warning("test 2")
5560
pymodbus_apply_logging_config(logging.NOTSET)
56-
Log.critical("test")
61+
Log.critical("test 3no")
5762
pymodbus_apply_logging_config("critical")
58-
Log.critical("test")
63+
Log.critical("test 3")
64+
Log.critical("test 3")
65+
Log.critical("test 3")
5966
pymodbus_apply_logging_config(logging.NOTSET)
60-
Log.error("test")
67+
Log.error("test 4no")
6168
pymodbus_apply_logging_config("error")
62-
Log.error("test")
69+
Log.error("test 4")
70+
Log.error("test 4")
71+
Log.error("test 4")
6372
pymodbus_apply_logging_config(logging.NOTSET)
64-
Log.critical("test")
73+
Log.info("test 5no")
74+
pymodbus_apply_logging_config("info")
75+
Log.info("test 5")
76+
Log.info("test 5")
77+
Log.info("test 5")

0 commit comments

Comments
 (0)