diff --git a/pymodbus/logging.py b/pymodbus/logging.py index fac314649..f49e8ed39 100644 --- a/pymodbus/logging.py +++ b/pymodbus/logging.py @@ -35,7 +35,6 @@ class Log: :meta private: """ - LOG_LEVEL = logging.NOTSET _logger = logging.getLogger(__name__) @classmethod @@ -59,7 +58,6 @@ def apply_logging_config(cls, level, log_file_name): def setLevel(cls, level): """Apply basic logging level""" cls._logger.setLevel(level) - cls.LOG_LEVEL = level @classmethod def build_msg(cls, txt, *args): @@ -90,39 +88,29 @@ def build_msg(cls, txt, *args): @classmethod def info(cls, txt, *args): """Log info messagees.""" - if cls.LOG_LEVEL == logging.NOTSET: - cls.LOG_LEVEL = cls._logger.getEffectiveLevel() - if logging.INFO >= cls.LOG_LEVEL: + if cls._logger.isEnabledFor(logging.INFO): cls._logger.info(cls.build_msg(txt, *args)) @classmethod def debug(cls, txt, *args): """Log debug messagees.""" - if cls.LOG_LEVEL == logging.NOTSET: - cls.LOG_LEVEL = cls._logger.getEffectiveLevel() - if logging.DEBUG >= cls.LOG_LEVEL: + if cls._logger.isEnabledFor(logging.DEBUG): cls._logger.debug(cls.build_msg(txt, *args)) @classmethod def warning(cls, txt, *args): """Log warning messagees.""" - if cls.LOG_LEVEL == logging.NOTSET: - cls.LOG_LEVEL = cls._logger.getEffectiveLevel() - if logging.WARNING >= cls.LOG_LEVEL: + if cls._logger.isEnabledFor(logging.WARNING): cls._logger.warning(cls.build_msg(txt, *args)) @classmethod def error(cls, txt, *args): """Log error messagees.""" - if cls.LOG_LEVEL == logging.NOTSET: - cls.LOG_LEVEL = cls._logger.getEffectiveLevel() - if logging.ERROR >= cls.LOG_LEVEL: + if cls._logger.isEnabledFor(logging.ERROR): cls._logger.error(cls.build_msg(txt, *args)) @classmethod def critical(cls, txt, *args): """Log critical messagees.""" - if cls.LOG_LEVEL == logging.NOTSET: - cls.LOG_LEVEL = cls._logger.getEffectiveLevel() - if logging.CRITICAL >= cls.LOG_LEVEL: + if cls._logger.isEnabledFor(logging.CRITICAL): cls._logger.critical(cls.build_msg(txt, *args)) diff --git a/test/test_logging.py b/test/test_logging.py index 52984262d..b1c5f911f 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -1,35 +1,25 @@ """Test datastore.""" import logging +from unittest.mock import patch import pytest -from pymodbus import pymodbus_apply_logging_config from pymodbus.logging import Log class TestLogging: """Tests of pymodbus logging.""" - def test_log_our_default(self): - """Test default logging""" - logging.getLogger().setLevel(logging.WARNING) - Log.setLevel(logging.NOTSET) - Log.info("test") - assert Log.LOG_LEVEL == logging.WARNING - Log.setLevel(logging.NOTSET) - logging.getLogger().setLevel(logging.INFO) - Log.info("test") - assert Log.LOG_LEVEL == logging.INFO - Log.setLevel(logging.NOTSET) - pymodbus_apply_logging_config() - assert Log.LOG_LEVEL == logging.DEBUG - - def test_log_set_level(self): - """Test default logging""" - pymodbus_apply_logging_config(logging.DEBUG) - assert Log.LOG_LEVEL == logging.DEBUG - pymodbus_apply_logging_config(logging.INFO) - assert Log.LOG_LEVEL == logging.INFO + def test_log_dont_call_build_msg(self): + """Verify that build_msg is not called unnecessary""" + with patch("pymodbus.logging.Log.build_msg") as build_msg_mock: + Log.setLevel(logging.INFO) + Log.debug("test") + build_msg_mock.assert_not_called() + + Log.setLevel(logging.DEBUG) + Log.debug("test2") + build_msg_mock.assert_called_once() def test_log_simple(self): """Test simple string"""