Skip to content

Commit 419e05b

Browse files
authored
fix(logger): log non-ascii characters as is when JSON stringifying (#3475)
* fix(parameters): make cache aware of single vs multiple calls Signed-off-by: heitorlessa <[email protected]> * chore: cleanup, add test for single and nested Signed-off-by: heitorlessa <[email protected]> * fix(logger): utf-8 encoding json Signed-off-by: heitorlessa <[email protected]> * chore: add all non_ascii compatible with 3.7+ * chore: mention issue for future debuggability --------- Signed-off-by: heitorlessa <[email protected]>
1 parent 8839f9c commit 419e05b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

aws_lambda_powertools/logging/formatter.py

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def __init__(
133133
default=self.json_default,
134134
separators=(",", ":"),
135135
indent=self.json_indent,
136+
ensure_ascii=False, # see #3474
136137
)
137138

138139
self.datefmt = datefmt

tests/functional/test_logger.py

+20
Original file line numberDiff line numberDiff line change
@@ -1120,3 +1120,23 @@ def filter(self, record):
11201120
log = capture_multiple_logging_statements_output(stdout)
11211121
assert log[0]["api_key"] == "REDACTED"
11221122
assert log[1]["api_key"] != "REDACTED"
1123+
1124+
1125+
def test_logger_json_unicode(stdout, service_name):
1126+
# GIVEN Logger is initialized
1127+
logger = Logger(service=service_name, stream=stdout)
1128+
1129+
# WHEN all non-ascii chars are logged as messages
1130+
# AND non-ascii is also used as ephemeral fields
1131+
# latest: https://www.unicode.org/versions/Unicode15.1.0/#Summary
1132+
non_ascii_chars = [chr(i) for i in range(128, 111_411_1)]
1133+
japanese_field = "47業レルし化"
1134+
japanese_string = "スコビルデモ2"
1135+
1136+
logger.info(non_ascii_chars, **{japanese_field: japanese_string})
1137+
1138+
# THEN JSON logs should not try to escape them
1139+
log = capture_logging_output(stdout)
1140+
1141+
assert log["message"] == non_ascii_chars
1142+
assert log[japanese_field] == japanese_string

0 commit comments

Comments
 (0)