Skip to content

Commit

Permalink
fix(logger): log non-ascii characters as is when JSON stringifying (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
heitorlessa authored Dec 11, 2023
1 parent 8839f9c commit 419e05b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit 419e05b

Please sign in to comment.