Skip to content

Commit

Permalink
Add JSON encoding default to encode items as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
reweeden committed Jan 28, 2022
1 parent 9c8a512 commit 5ed31c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rain_api_core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def format(self, record: logging.LogRecord) -> str:
obj = self.formatMessage(record)
assert not any(isinstance(val, PercentPlaceholder) for val in _walk_json_values(obj))

return filter_log_credentials(json.dumps(obj))
return filter_log_credentials(json.dumps(obj, default=str))


class TaggingFilter(logging.Filter):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ def test_json_logging_quotes_malformed(logger, log_io):
assert json.loads(msg) == {"message": obj}


def test_json_logging_not_serializable(logger, log_io):
class SomeClass():
def __repr__(self) -> str:
return "SomeClass()"

logger.info(SomeClass())

msg = log_io.getvalue()
assert msg == '{"message": "SomeClass()"}\n'
assert json.loads(msg) == {"message": "SomeClass()"}


def test_json_logging_missing_key(logger, custom_log_handler, log_io):
custom_log_handler.setFormatter(JSONFormatter("%(does_not_exist)s"))
logger.info("hello")
Expand Down

0 comments on commit 5ed31c4

Please sign in to comment.