Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ def __init__(
style="%",
validate=None,
stack_trace_limit=None,
extra=None,
exclude_fields=(),
):
# type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str]) -> None
# type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Dict[str], Sequence[str]) -> None
"""Initialize the ECS formatter.

:param int stack_trace_limit:
Expand All @@ -89,6 +90,8 @@ def __init__(
Setting this to zero will suppress stack traces.
This setting doesn't affect ``LogRecord.stack_info`` because
this attribute is typically already pre-formatted.
:param Dict[str] extra:
Specifies the collection of meta-data fields to add to all records.
:param Sequence[str] exclude_fields:
Specifies any fields that should be suppressed from the resulting
fields, expressed with dot notation::
Expand Down Expand Up @@ -129,6 +132,7 @@ def __init__(
):
raise TypeError("'exclude_fields' must be a sequence of strings")

self._extra = extra
self._exclude_fields = frozenset(exclude_fields)
self._stack_trace_limit = stack_trace_limit

Expand Down Expand Up @@ -218,6 +222,10 @@ def format_to_ecs(self, record):
# since they can be defined as 'extras={"http": {"method": "GET"}}'
extra_keys = set(available).difference(self._LOGRECORD_DICT)
extras = flatten_dict({key: available[key] for key in extra_keys})
# Merge in any global extra's
if self._extra is not None:
for field, value in self._extra.items():
merge_dicts(de_dot(field, value), extras)

# Pop all Elastic APM extras and add them
# to standard tracing ECS fields.
Expand Down
14 changes: 13 additions & 1 deletion tests/test_stdlib_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import ecs_logging
from .compat import StringIO


requires_py3 = pytest.mark.skipif(
sys.version_info[0] < 3, reason="Test requires Python 3.x+"
)
Expand Down Expand Up @@ -63,6 +62,19 @@ def test_record_formatted(spec_validator):
)


def test_extra_global_is_merged(spec_validator):
formatter = ecs_logging.StdlibFormatter(
exclude_fields=["process"], extra={"environment": "dev"}
)

assert spec_validator(formatter.format(make_record())) == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs":{"version":"1.6.0"},'
'"environment":"dev",'
'"log":{"logger":"logger-name","origin":{"file":{"line":10,"name":"file.py"},"function":"test_function"},'
'"original":"1: hello"}}'
)


def test_can_be_overridden(spec_validator):
class CustomFormatter(ecs_logging.StdlibFormatter):
def format_to_ecs(self, record):
Expand Down