Logging the literal string "None" is silently dropped (payload becomes empty)

未关闭 适合新手
#17,339 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
python
领域
observability

调研方向

从 google/cloud/logging_v2/handlers/handlers.py 中的 _format_and_parse_message 开始,然后运行针对 "None"、"hello" 和 "" 提供的 LogRecord 复现。完成的标准是:字面字符串 "None" 保留在解析后的 payload 中,同时现有用例保留其预期结果。

由索引模型根据 Issue 内容生成。

描述

api: logging priority: p3 type: bug
  • I determined this is the correct repository in which to report this bug.

Summary of the issue

Context
Using CloudLoggingHandler / StructuredLogHandler via the Python standard logging module, I logged the string "None" (e.g. logging.getLogger().info("None")).

Expected Behavior:
The log entry payload should contain the text "None", just like any other string.

Actual Behavior:
The message is silently dropped: the parsed payload becomes None (empty), so the log content disappears.

API client name and version

google-cloud-logging v3.15.0 (also present on current main)

Reproduction steps: code

import logging
from google.cloud.logging_v2.handlers import handlers as H

class FakeHandler(logging.Handler):
    pass

h = FakeHandler()
h.setFormatter(logging.Formatter())

def rec(msg):
    return logging.LogRecord("n", logging.INFO, "p", 1, msg, (), None)

for msg in ["None", "hello", ""]:
    print(repr(msg), "->", repr(H._format_and_parse_message(rec(msg), h)))

Reproduction steps: actual results

'None'  -> None      # the user's message is dropped
'hello' -> 'hello'
''      -> ''

Reproduction steps: expected results

'None'  -> 'None'    # the literal string should be preserved
'hello' -> 'hello'
''      -> ''

OS & version + platform

Any (logic bug, platform-independent)

Python environment

Python 3.12

Additional context

Root cause in google/cloud/logging_v2/handlers/handlers.py (_format_and_parse_message):

if message != "None":
    passed_json_fields["message"] = message
...
return message if message != "None" else None

The intent appears to be detecting a record whose msg is the Python object None (which logging.Formatter renders as the string "None"). But comparing the formatted string against "None" also matches a legitimate user message of the literal text "None", dropping it. Detecting emptiness from record.msg is None (before formatting) rather than from the formatted output would avoid the false positive.

主要语言
Python
星标
5.4k
派生
1.8k
平均合并
1 天 17 小时
30 天内合并 PR
93

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

googleapis/google-cloud-python 的其他 Issue

查看 googleapis/google-cloud-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。