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 default/fallback behavior for json.dumps #47

Merged
merged 10 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Add default/fallback handling for json.dumps ([#47](https://github.com/elastic/ecs-logging-python/pull/47))

## 1.0.0 (2021-02-08)

* Remove "beta" designation
Expand Down
35 changes: 32 additions & 3 deletions ecs_logging/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,46 @@ def json_dumps(value):
# case the given values aren't strings (even though
# they should be according to the spec)
ordered_json = ",".join(
'"%s":%s' % (k, json.dumps(v, sort_keys=True, separators=(",", ":")))
'"%s":%s'
% (
k,
json.dumps(
v,
sort_keys=True,
separators=(",", ":"),
default=_json_dumps_fallback,
),
)
for k, v in ordered_fields
)
if value:
return "{%s,%s" % (
ordered_json,
json.dumps(value, sort_keys=True, separators=(",", ":"))[1:],
json.dumps(
value,
sort_keys=True,
separators=(",", ":"),
default=_json_dumps_fallback,
)[1:],
)
else:
return "{%s}" % ordered_json
# If there are no fields with ordering requirements we
# pass everything into json.dumps()
else:
return json.dumps(value, sort_keys=True, separators=(",", ":"))
return json.dumps(
value, sort_keys=True, separators=(",", ":"), default=_json_dumps_fallback
)


def _json_dumps_fallback(value):
# type: (Any) -> Any
"""
Fallback handler for json.dumps to handle objects json doesn't know how to
serialize.
"""
try:
# This is what structlog's json fallback does
return value.__structlog__()
except AttributeError:
return repr(value)
15 changes: 13 additions & 2 deletions tests/test_structlog_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@
from .compat import StringIO


class NotSerializable:
def __repr__(self):
return "<NotSerializable>"


def make_event_dict():
return {"event": "test message", "log.logger": "logger-name"}
return {
"event": "test message",
"log.logger": "logger-name",
"baz": NotSerializable(),
}


@mock.patch("time.time")
Expand All @@ -32,7 +41,9 @@ def test_event_dict_formatted(time, spec_validator):
formatter = ecs_logging.StructlogFormatter()
assert spec_validator(formatter(None, "debug", make_event_dict())) == (
'{"@timestamp":"2020-03-20T16:16:37.187Z","log.level":"debug",'
'"message":"test message","ecs":{"version":"1.6.0"},'
'"message":"test message",'
'"baz":"<NotSerializable>",'
'"ecs":{"version":"1.6.0"},'
'"log":{"logger":"logger-name"}}'
)

Expand Down