Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pymodbus/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
33 changes: 23 additions & 10 deletions test/not_updated/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")