Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for adding callables as extra fields #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should catch errors from the callable (and if so, move it into a seperate method).

If the callable throws an error, this way the whole log event will be lost.
If the error is handled, the log event can be sent anyways and we could log the error for later analysis.

What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piotrmaslanka what do you think?

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