diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d274fd..33c01fe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed: + +- fixed a bug where `log_prefix` can only be a string [#393](https://github.com/Draegerwerk/sdc11073/issues/393) + ## [2.1.0] - 2024-09-04 ### Added diff --git a/src/sdc11073/loghelper.py b/src/sdc11073/loghelper.py index 44b61dd3..8639cc8f 100644 --- a/src/sdc11073/loghelper.py +++ b/src/sdc11073/loghelper.py @@ -69,7 +69,7 @@ def __init__(self, logger, prefix=None): def _process(self, msg, args, kwargs): try: - _msg = self.log_prefix + msg + _msg = f'{self.log_prefix}{msg}' except TypeError: _msg = msg diff --git a/tests/test_loghelper.py b/tests/test_loghelper.py index 084d5800..a0d7a386 100644 --- a/tests/test_loghelper.py +++ b/tests/test_loghelper.py @@ -1,5 +1,7 @@ import logging import unittest +import uuid +from unittest import mock from sdc11073 import loghelper @@ -47,3 +49,15 @@ def test_logwatcher(self): records = lw2.getAllRecords() self.assertEqual(len(records), 1) self.assertRaises(loghelper.LogWatchError, lw2.check) + + def test_ident_parameter(self): + def _test_prefix(prefix): + adapter = loghelper.LoggerAdapter(logger=mock.MagicMock(), prefix=prefix) + msg = uuid.uuid4() + processed_msg = adapter._process(msg, (), ()) + self.assertEqual(f'{prefix or ""}{msg}', processed_msg) + + _test_prefix(1) + _test_prefix('1') + _test_prefix(mock.MagicMock()) + _test_prefix(None)