Skip to content

Commit 625b7ab

Browse files
Skip interpolation if args is empty (#15)
1 parent 075554f commit 625b7ab

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ on:
1414

1515
jobs:
1616
call_ci:
17-
uses: EffectiveRange/ci-workflows/.github/workflows/python-ci.yaml@v3
17+
uses: EffectiveRange/ci-workflows/.github/workflows/python-ci.yaml@v5

context_logger/filter.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ def __init__(self, application_name: str, message_field: str):
1515
self._message_field = message_field
1616

1717
def filter(self, record: LogRecord) -> bool:
18-
if not isinstance(record.msg, dict):
19-
record.msg = {self._message_field: record.msg % record.args}
20-
record.args = ()
21-
22-
record.msg['hostname'] = socket.gethostname()
23-
record.msg['application'] = self._application_name
24-
record.msg['app_version'] = self._get_application_version()
25-
26-
if 'process_name' in record.msg:
27-
record.msg['process_name'] = record.processName
18+
try:
19+
if isinstance(record.msg, str):
20+
record.msg = {self._message_field: record.msg % record.args if record.args else record.msg}
21+
record.args = ()
22+
23+
record.msg['hostname'] = socket.gethostname()
24+
record.msg['application'] = self._application_name
25+
record.msg['app_version'] = self._get_application_version()
26+
27+
if 'process_name' in record.msg:
28+
record.msg['process_name'] = record.processName
29+
except Exception as exception:
30+
print('Failed to handle log record:', exception)
2831

2932
return True
3033

tests/loggerTest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_file_logging(self):
6262
# When
6363
log.info('This is a simple message')
6464
stdlib_log.info('This is a %s message', 'simple')
65+
stdlib_log.info('This is a %s message', )
6566
log.error('This is an error message', error_message='Something terrible happened', error_code=1234)
6667
stdlib_log.error('This is an error message')
6768

@@ -74,6 +75,9 @@ def test_file_logging(self):
7475
log_entry = json.loads(log_file.readline())
7576
assert_simple_message(self, log_entry)
7677

78+
log_entry = json.loads(log_file.readline())
79+
self.assertEqual('This is a %s message', log_entry.get('message'))
80+
7781
log_entry = json.loads(log_file.readline())
7882
self.assertEqual('This is an error message', log_entry.get('message'))
7983
self.assertEqual('Something terrible happened', log_entry.get('error_message'))

0 commit comments

Comments
 (0)