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 Global context fields to StdlibFormatter #65

Merged
merged 7 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 16 additions & 8 deletions ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ class StdlibFormatter(logging.Formatter):
converter = time.gmtime

def __init__(
self,
fmt=None,
datefmt=None,
style="%",
validate=None,
stack_trace_limit=None,
exclude_fields=(),
self, # type: Any
fmt=None, # type: Optional[str]
datefmt=None, # type: Optional[str]
style="%", # type: str
validate=None, # type: Optional[bool]
stack_trace_limit=None, # type: Optional[int]
extra=None, # type: Optional[Dict[Any, Any]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be Optional[Dict[str, Any]]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sethmlarson - That's a good point, @basepi should I change it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rdifrango Yes please!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, now let's hope it builds ;)

exclude_fields=(), # type: Sequence[str]
):
# type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str]) -> None
# type: (...) -> 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