Skip to content

Commit

Permalink
Add support for adding callables as extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmaslanka committed Mar 20, 2024
1 parent 49635e1 commit 666455f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ Options:
to Logstash. This dictionary will be merged with any other extra
items passed in the logging call.

Note that you can also put a callable with zero arguments. If that is
the case, the callable will be evaluated at the moment you log this thing
(ie. not in the submitter thread). If this callable returns None, extra
field will be skipped.

*Type*: ``dict``

*Default*: None
Expand Down
6 changes: 5 additions & 1 deletion logstash_async/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ def _get_extra_fields(self, record):
}
# static extra fields
if self._extra:
extra_fields.update(self._extra)
for field_name, field_value in self._extra.items():
if callable(field_value):
field_value = field_value()
if field_value is not None:
extra_fields[field_name] = field_value
if getattr(record, 'taskName', None):
extra_fields[Schema.TASK_NAME] = record.taskName
# exceptions
Expand Down
3 changes: 2 additions & 1 deletion tests/formatter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_format_timestamp_microsecond_2(self):

@patch.object(LogstashFormatter, '_format_exception', lambda s, e: e)
def test_default_schema(self):
formatter = LogstashFormatter(tags=['t1', 't2'])
formatter = LogstashFormatter(tags=['t1', 't2'], extra={'value': lambda: 5})
result = formatter._format_to_dict(create_log_record())
self.assertDictEqual(result, {
'@timestamp': '2021-10-24T13:32:15.024Z',
Expand All @@ -108,6 +108,7 @@ def test_default_schema(self):
'type': 'python-logstash',
'tags': ['t1', 't2'],
'extra': {
'value': 5,
'func_name': 'f',
'interpreter': sys.executable,
'interpreter_version': _interpreter_version,
Expand Down

0 comments on commit 666455f

Please sign in to comment.