logging should cache LogRecord.message
オープン
まだ誰も着手していません。
stdlib
type-feature
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
logging currently does not cache LogRecord.message:
https://github.com/python/cpython/blob/0dcbc8385322ff51f7fc3e586027d880275df4fa/Lib/logging/__init__.py#L391-L401
https://github.com/python/cpython/blob/0dcbc8385322ff51f7fc3e586027d880275df4fa/Lib/logging/__init__.py#L711
Since logging caches other string-formatting related results such as LogRecord.asctime and LogRecord.exc_text, I would like to suggest:
LogRecord.getMessageshould checkself.messagefirst. If it is not computed yet, it should compute the message and set this attribute before returning.Formattershouldn't setrecord.message. It only needs to callLogRecord.getMessageto get the message.
How to reproduce
import logging
class A:
def __repr__(self):
print('repr called')
return '<A>'
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.StreamHandler())
logger.error('%r', A())
Expected output
repr called
<A>
<A>
Actual output
repr called
<A>
repr called
<A>
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-134367
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Lib/logging/init.py の参照されている LogRecord と Formatter の箇所を読み、その後 reproduction を実行して repr が繰り返し呼び出されることを確認します。開始する前に、リンクされた PR gh-134367 を確認してください。2 つの handler の例で repr が 1 回だけ呼び出され、その間に両方の handler がメッセージを生成すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- observability
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 30/100