Skip to content

Commit

Permalink
allow any type for log prefix (Draegerwerk#395)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the title above -->
<!--- Link the corresponding issues after you created the pull request
-->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [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:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have updated the [changelog](../CHANGELOG.md) accordingly.
- [x] I have added tests to cover my changes.
  • Loading branch information
leon1995 authored Sep 16, 2024
1 parent 61ffb44 commit c5ca3f2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/sdc11073/loghelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions tests/test_loghelper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import unittest
import uuid
from unittest import mock

from sdc11073 import loghelper

Expand Down Expand Up @@ -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)

0 comments on commit c5ca3f2

Please sign in to comment.