Repository navigation
fix(logger): keep string messages that are valid JSON scalars as strings - #8838
Open
saquibjawedbit wants to merge 1 commit into
Open
saquibjawedbit wants to merge 1 commit into
saquibjawedbit wants to merge 1 commit into
Conversation
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Issue number:
closes #8828
Summary
Changes
LambdaPowertoolsFormatter._extract_log_message(aws_lambda_powertools/logging/formatter.py) triedjson.loadson every string message and kept whatever came back. It now keeps the decoded value only when it is adictor alist; for any other result (null, bool, number, JSON string) it keeps the original string. Decoding JSON objects and arrays into structured output, the documented feature, is unchanged.Tests added in
tests/functional/logger/required_dependencies/test_logger_powertools_formatter.py, through the publicLoggerAPI only:test_with_json_scalar_string_message_stays_string, parametrized with"null","true","123","1e3",'"quoted"',"NaN"test_with_json_object_or_array_string_message_is_decoded, a JSON object string and a top-level JSON array string are still decodedNo existing tests were changed.
User experience
Before:
After, all four log
"message": "<the original string>".logger.info('{"a": 1}')andlogger.info('[1, 2]')still log structured{"a": 1}/[1, 2].This is the bug fix as triaged in the issue. Other user-visible effects of the same rule, all covered by tests or checked by hand:
logger.info('"quoted"')now keeps the quotes instead of loggingquoted.logger.info("NaN")now logs"message": "NaN"instead of a bareNaN, which made the whole line invalid JSON.json_deserializerthat returns something other than adict/list(e.g. a tuple) now leaves the message as the original string.' {"a": 1} ') are still decoded;bytesmessages are untouched.DatadogLogFormatterinherits the method, so it gets the same fix.Verification
AI-assisted; I ran the tests below myself and pasted the output.
Local run on macOS, Python 3.13.15, environment from
make dev.New tests on
develop, before the fix (uv run --locked pytest tests/functional/logger/required_dependencies/test_logger_powertools_formatter.py -k "json_scalar or json_object_or_array"):Same tests after the fix (same command with
-v):Logger unit + functional suites (
uv run --locked pytest tests/unit/logger tests/functional/logger):Lint, format, types (
make format-check lint-check,make mypy):By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.