Skip to content

Commit bd6ecb5

Browse files
wlcrsjaniversen
authored andcommitted
Remove unneccesary custom LOG_LEVEL check (#1424)
1 parent 5cd31c4 commit bd6ecb5

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

pymodbus/logging.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Log:
3535
:meta private:
3636
"""
3737

38-
LOG_LEVEL = logging.NOTSET
3938
_logger = logging.getLogger(__name__)
4039

4140
@classmethod
@@ -59,7 +58,6 @@ def apply_logging_config(cls, level, log_file_name):
5958
def setLevel(cls, level):
6059
"""Apply basic logging level"""
6160
cls._logger.setLevel(level)
62-
cls.LOG_LEVEL = level
6361

6462
@classmethod
6563
def build_msg(cls, txt, *args):
@@ -90,39 +88,29 @@ def build_msg(cls, txt, *args):
9088
@classmethod
9189
def info(cls, txt, *args):
9290
"""Log info messagees."""
93-
if cls.LOG_LEVEL == logging.NOTSET:
94-
cls.LOG_LEVEL = cls._logger.getEffectiveLevel()
95-
if logging.INFO >= cls.LOG_LEVEL:
91+
if cls._logger.isEnabledFor(logging.INFO):
9692
cls._logger.info(cls.build_msg(txt, *args))
9793

9894
@classmethod
9995
def debug(cls, txt, *args):
10096
"""Log debug messagees."""
101-
if cls.LOG_LEVEL == logging.NOTSET:
102-
cls.LOG_LEVEL = cls._logger.getEffectiveLevel()
103-
if logging.DEBUG >= cls.LOG_LEVEL:
97+
if cls._logger.isEnabledFor(logging.DEBUG):
10498
cls._logger.debug(cls.build_msg(txt, *args))
10599

106100
@classmethod
107101
def warning(cls, txt, *args):
108102
"""Log warning messagees."""
109-
if cls.LOG_LEVEL == logging.NOTSET:
110-
cls.LOG_LEVEL = cls._logger.getEffectiveLevel()
111-
if logging.WARNING >= cls.LOG_LEVEL:
103+
if cls._logger.isEnabledFor(logging.WARNING):
112104
cls._logger.warning(cls.build_msg(txt, *args))
113105

114106
@classmethod
115107
def error(cls, txt, *args):
116108
"""Log error messagees."""
117-
if cls.LOG_LEVEL == logging.NOTSET:
118-
cls.LOG_LEVEL = cls._logger.getEffectiveLevel()
119-
if logging.ERROR >= cls.LOG_LEVEL:
109+
if cls._logger.isEnabledFor(logging.ERROR):
120110
cls._logger.error(cls.build_msg(txt, *args))
121111

122112
@classmethod
123113
def critical(cls, txt, *args):
124114
"""Log critical messagees."""
125-
if cls.LOG_LEVEL == logging.NOTSET:
126-
cls.LOG_LEVEL = cls._logger.getEffectiveLevel()
127-
if logging.CRITICAL >= cls.LOG_LEVEL:
115+
if cls._logger.isEnabledFor(logging.CRITICAL):
128116
cls._logger.critical(cls.build_msg(txt, *args))

test/test_logging.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
"""Test datastore."""
22
import logging
3+
from unittest.mock import patch
34

45
import pytest
56

6-
from pymodbus import pymodbus_apply_logging_config
77
from pymodbus.logging import Log
88

99

1010
class TestLogging:
1111
"""Tests of pymodbus logging."""
1212

13-
def test_log_our_default(self):
14-
"""Test default logging"""
15-
logging.getLogger().setLevel(logging.WARNING)
16-
Log.setLevel(logging.NOTSET)
17-
Log.info("test")
18-
assert Log.LOG_LEVEL == logging.WARNING
19-
Log.setLevel(logging.NOTSET)
20-
logging.getLogger().setLevel(logging.INFO)
21-
Log.info("test")
22-
assert Log.LOG_LEVEL == logging.INFO
23-
Log.setLevel(logging.NOTSET)
24-
pymodbus_apply_logging_config()
25-
assert Log.LOG_LEVEL == logging.DEBUG
26-
27-
def test_log_set_level(self):
28-
"""Test default logging"""
29-
pymodbus_apply_logging_config(logging.DEBUG)
30-
assert Log.LOG_LEVEL == logging.DEBUG
31-
pymodbus_apply_logging_config(logging.INFO)
32-
assert Log.LOG_LEVEL == logging.INFO
13+
def test_log_dont_call_build_msg(self):
14+
"""Verify that build_msg is not called unnecessary"""
15+
with patch("pymodbus.logging.Log.build_msg") as build_msg_mock:
16+
Log.setLevel(logging.INFO)
17+
Log.debug("test")
18+
build_msg_mock.assert_not_called()
19+
20+
Log.setLevel(logging.DEBUG)
21+
Log.debug("test2")
22+
build_msg_mock.assert_called_once()
3323

3424
def test_log_simple(self):
3525
"""Test simple string"""

0 commit comments

Comments
 (0)