-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add json_fields extras argument for adding to jsonPayload #447
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
Changes from all commits
4ec8795
6ac7658
f8107cb
c147da3
538fc09
a7b811c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,9 +221,16 @@ def _format_and_parse_message(record, formatter_handler): | |
record (logging.LogRecord): The record object representing the log | ||
formatter_handler (logging.Handler): The handler used to format the log | ||
""" | ||
# if message is a dictionary, return as-is | ||
passed_json_fields = getattr(record, "json_fields", {}) | ||
# if message is a dictionary, use dictionary directly | ||
if isinstance(record.msg, collections.abc.Mapping): | ||
return record.msg | ||
payload = record.msg | ||
# attach any extra json fields if present | ||
if passed_json_fields and isinstance( | ||
passed_json_fields, collections.abc.Mapping | ||
Comment on lines
+229
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if passed_json_fields is undefined (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand. Line 226 should be unrelated to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to improve the comments here |
||
): | ||
payload = {**payload, **passed_json_fields} | ||
return payload | ||
# format message string based on superclass | ||
message = formatter_handler.format(record) | ||
try: | ||
|
@@ -235,6 +242,11 @@ def _format_and_parse_message(record, formatter_handler): | |
except (json.decoder.JSONDecodeError, IndexError): | ||
# log string is not valid json | ||
pass | ||
# if json_fields was set, create a dictionary using that | ||
if passed_json_fields and isinstance(passed_json_fields, collections.abc.Mapping): | ||
if message != "None": | ||
passed_json_fields["message"] = message | ||
return passed_json_fields | ||
# if formatted message contains no content, return None | ||
return message if message != "None" else None | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,3 +321,26 @@ def test_format_overrides(self): | |
result = json.loads(handler.format(record)) | ||
for (key, value) in expected_payload.items(): | ||
self.assertEqual(value, result[key]) | ||
|
||
def test_format_with_json_fields(self): | ||
""" | ||
User can add json_fields to the record, which should populate the payload | ||
""" | ||
import logging | ||
import json | ||
|
||
handler = self._make_one() | ||
message = "name: %s" | ||
name_arg = "Daniel" | ||
Comment on lines
+333
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: i am not familiar with the interface, is it supposed to format the message like with print() ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is that Python's logging std lib allows you to build formated log strings like this: |
||
expected_result = "name: Daniel" | ||
json_fields = {"hello": "world", "number": 12} | ||
record = logging.LogRecord( | ||
None, logging.INFO, None, None, message, name_arg, None, | ||
) | ||
record.created = None | ||
setattr(record, "json_fields", json_fields) | ||
handler.filter(record) | ||
result = json.loads(handler.format(record)) | ||
self.assertEqual(result["message"], expected_result) | ||
self.assertEqual(result["hello"], "world") | ||
self.assertEqual(result["number"], 12) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think that I saw this commit in PR #450. Is it possible there had to be a rebase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it seems like there was a merging issue in the v3.0.0 branch at some point which changed this line, and I had to fix it in both branches for tests to pass