Skip to content

fix(logger): log non-ascii characters as is when JSON stringifying #3475

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions aws_lambda_powertools/logging/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(
default=self.json_default,
separators=(",", ":"),
indent=self.json_indent,
ensure_ascii=False, # see #3474
)

self.datefmt = datefmt
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,23 @@ def filter(self, record):
log = capture_multiple_logging_statements_output(stdout)
assert log[0]["api_key"] == "REDACTED"
assert log[1]["api_key"] != "REDACTED"


def test_logger_json_unicode(stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)

# WHEN all non-ascii chars are logged as messages
# AND non-ascii is also used as ephemeral fields
# latest: https://www.unicode.org/versions/Unicode15.1.0/#Summary
non_ascii_chars = [chr(i) for i in range(128, 111_411_1)]
japanese_field = "47業レルし化"
japanese_string = "スコビルデモ2"

logger.info(non_ascii_chars, **{japanese_field: japanese_string})

# THEN JSON logs should not try to escape them
log = capture_logging_output(stdout)

assert log["message"] == non_ascii_chars
assert log[japanese_field] == japanese_string