From c3e989a0b388b3ae28393d44e771c3127fd878b8 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 16 Sep 2024 13:58:07 +0200 Subject: [PATCH] allow any type for log prefix v1 (#394) ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: - [x] I have updated the [changelog](../CHANGELOG.md) accordingly. - [x] I have added tests to cover my changes. --- CHANGELOG.md | 1 + src/sdc11073/loghelper.py | 2 +- tests/test_loghelper.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed22e937..e0b7d80e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - accessing a multikey may lead to IndexError [#359](https://github.com/Draegerwerk/sdc11073/issues/359) +- fixed a bug where `log_prefix` can only be a string [#393](https://github.com/Draegerwerk/sdc11073/issues/393) ## [1.3.2] - 2024-03-18 diff --git a/src/sdc11073/loghelper.py b/src/sdc11073/loghelper.py index 3e38bcf4..ceb9bbba 100644 --- a/src/sdc11073/loghelper.py +++ b/src/sdc11073/loghelper.py @@ -60,7 +60,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 ef04e7b9..f4248a3c 100644 --- a/tests/test_loghelper.py +++ b/tests/test_loghelper.py @@ -1,5 +1,8 @@ import unittest import logging +import uuid +from unittest import mock + from sdc11073 import loghelper @@ -45,3 +48,15 @@ def test_logwatcher(self): records = lw2.getAllRecords() self.assertEqual(len(records), 1) self.assertRaises(loghelper.LogWatchException, 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)