fix(logging): preserve log messages with the literal string "None"#17340
fix(logging): preserve log messages with the literal string "None"#17340o6ivp wants to merge 1 commit into
Conversation
`_format_and_parse_message` treated any record whose formatted output
equaled the string "None" as empty, returning `None` (or omitting the
`message` field when `json_fields` were present). This also dropped a
legitimate user message of the literal string "None"
(e.g. `logging.getLogger().info("None")`).
The empty-content case is meant to cover a record whose `msg` is the
Python object `None`, which `logging.Formatter` renders as "None".
Detect that from `record.msg is None` directly so the literal string
"None" is preserved.
Fixes googleapis#17339
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where a literal string message of "None" was incorrectly treated as an empty message and dropped. It introduces a check to ensure that only records where the original message (record.msg) is actually the Python None object are treated as empty, while preserving the literal string "None". Unit tests have been added to verify this behavior. There are no review comments to address, and the changes look solid.
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #17339 🦕
What
_format_and_parse_message(inhandlers/handlers.py) treated any record whose formatted output equaled the string"None"as empty content, returningNone(or omitting themessagefield whenjson_fieldswere present).This also dropped a legitimate user message consisting of the literal string
"None":Why
The empty-content branch is intended to cover a record whose
msgis the Python objectNone, whichlogging.Formatterrenders as the string"None". Comparing the formatted string against"None"cannot distinguish that case from a user who logged the literal text"None", so real content was lost.How
Detect the empty case from
record.msg is None(before formatting) and the formatted output being"None", instead of from the formatted output alone. This preserves the existing behavior formsg=None(with and without a custom formatter) while keeping a genuine"None"string.Behavior matrix:
record.msgNoneNoneNone(unchanged)None"name: %(name)s")"name: logname""name: logname"(unchanged)"None"(str)None❌ dropped"None"✅ preservedTests
Added two unit tests (
test_literal_none_string_preserved,test_literal_none_string_preserved_with_json_fields). Alltests/unit/handlers/tests pass (177 passed) andruff format --checkis clean.