slackapi / slackapi/python-slack-sdk
Use logger.isEnabledFor(logging.DEBUG) instead of logger.level <= logging.DEBUG for debug guards
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 4k
- フォーク
- 857
- 平均マージ
- 22時間 21分
- マージ済み PR(30日)
- 16
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず slack_sdk/ 配下で logger.level <= logging.DEBUG を検索し、次に slack_sdk/socket_mode/ の Socket Mode の例、builtin/client.py と client.py を含めて調査します。この変更を Socket Mode のみに適用するのか、約 88 個すべての出現箇所に適用するのかを判断してください。完了条件は、デバッグチェックが有効な logger レベルを使用し、プロジェクト全体の出現箇所が一貫して対応されていることです。高コストな処理を回避する場合にのみ guards を残します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- api, backend
- issue の種類
- リファクタリング
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 55/100