slackapi / slackapi/python-slack-sdk

Use logger.isEnabledFor(logging.DEBUG) instead of logger.level <= logging.DEBUG for debug guards

Abierto
#1,957 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

auto-triage-skip bug
Lenguaje dominante
Python
Estrellas
4k
Forks
857
Merge medio
22 h 21 min
PR fusionados (30 d)
16

Descripción

Summary

Throughout Socket Mode, debug logs are guarded like this:

if self.logger.level <= logging.DEBUG:
    self.logger.debug(f"... {expensive_call()} ...")

The guard exists to avoid building the message string when debug is off (the f-string argument is evaluated eagerly, before debug() can no-op it). That's a valid goal — e.g. builtin/client.py calls debug_redacted_message_string(message), and client.py calls self.message_queue.qsize() inside the message.

But logger.level is the wrong check: it's only the level explicitly set on that exact logger, defaulting to NOTSET (0). These loggers are created with logging.getLogger(__name__) and setLevel() is never called on them. So with the usual logging.basicConfig(level=logging.INFO) (which configures the root logger), logger.level stays 0, 0 <= 10 is always True, and the guard passes anyway — the expensive string still gets built. The optimization silently does nothing in the most common setup.

Suggested change

Replace:

if self.logger.level <= logging.DEBUG:

with:

if self.logger.isEnabledFor(logging.DEBUG):

isEnabledFor() uses the effective level (walking up the logger hierarchy via getEffectiveLevel()), so it correctly short-circuits when logging is configured at the root/parent — which is what the guard was meant to do.

Where the guarded message is cheap (e.g. it only interpolates an already-computed value), the guard could simply be dropped instead.

Scope

Spotted in slack_sdk/socket_mode/, but the same logger.level <= logging.DEBUG idiom appears ~88 times across ~23 files in slack_sdk/ (webhook, scim, web, audit_logs, rtm, oauth, …). isEnabledFor is currently used nowhere. Worth deciding whether to fix Socket Mode only or apply the change project-wide.

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.

Línea de trabajo

Empieza buscando logger.level <= logging.DEBUG en slack_sdk/ y luego inspecciona los ejemplos de Socket Mode en slack_sdk/socket_mode/, incluidos builtin/client.py y client.py. Decide si el cambio se aplica solo a Socket Mode o a las aproximadamente 88 apariciones; se considera terminado cuando las comprobaciones de depuración usan los niveles efectivos del logger y las apariciones en todo el proyecto se han tratado de forma coherente, conservando las guards solo cuando evitan trabajo costoso.

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

Evaluación

Stack tecnológico
python
Área
api, backend
Tipo de issue
Refactorización
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Activo
Claridad
Bastante claro
Aptitud para principiantes
55/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.