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
22 changes: 5 additions & 17 deletions pymodbus/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Log:
:meta private:
"""

LOG_LEVEL = logging.NOTSET
_logger = logging.getLogger(__name__)

@classmethod
Expand All @@ -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):
Expand Down Expand Up @@ -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))
32 changes: 11 additions & 21 deletions test/test_logging.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down