Repository navigation
fix(logger): keep string messages that are valid JSON scalars as strings - #8838
Conversation
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @saquibjawedbit! Keeping only dicts and lists is the whole fix, and the parametrized test pins the documented behaviour next to it. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8838 +/- ##
========================================
Coverage 97.20% 97.20%
========================================
Files 327 327
Lines 16759 16761 +2
Branches 1522 1523 +1
========================================
+ Hits 16290 16292 +2
Misses 328 328
Partials 141 141 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



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.