logging should cache LogRecord.message
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Đọc Lib/logging/init.py tại các vị trí LogRecord và Formatter được tham chiếu, sau đó chạy reproduction để quan sát các lần gọi repr lặp lại. Xem xét PR được liên kết gh-134367 trước khi bắt đầu. Hoàn thành khi ví dụ với hai handler gọi repr một lần trong khi cả hai handler đều tạo ra thông báo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- observability
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 30/100