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

Abierto Apto para principiantes
#17,339 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
85/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
python

Línea de trabajo

Comienza en google/cloud/logging_v2/handlers/handlers.py, en _format_and_parse_message, y luego ejecuta la reproducción proporcionada de LogRecord para "None", "hello" y "". Se considera terminado cuando la cadena literal "None" permanece en el payload analizado, mientras los casos existentes conservan sus resultados esperados.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
5.4k
Forks
1.8k
Merge medio
1 d 17 h
PR fusionados (30 d)
93

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de googleapis/google-cloud-python

Todos los issues de googleapis/google-cloud-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.