From b625bdc2d339ca3da3bcb75d1df17eccc066a603 Mon Sep 17 00:00:00 2001 From: Piotr Maslanka Date: Wed, 20 Mar 2024 14:50:38 +0100 Subject: [PATCH] Add support for adding callables as extra fields --- docs/config.rst | 5 +++++ logstash_async/formatter.py | 6 +++++- tests/formatter_test.py | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index c44b8fb..e1fe543 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -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 diff --git a/logstash_async/formatter.py b/logstash_async/formatter.py index cc9cf09..00a6ca0 100644 --- a/logstash_async/formatter.py +++ b/logstash_async/formatter.py @@ -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 diff --git a/tests/formatter_test.py b/tests/formatter_test.py index c1d916e..8ea4c29 100644 --- a/tests/formatter_test.py +++ b/tests/formatter_test.py @@ -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', @@ -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,