-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
Bug report
Bug description:
logging currently does not cache LogRecord.message:
cpython/Lib/logging/__init__.py
Lines 391 to 401 in 0dcbc83
| def getMessage(self): | |
| """ | |
| Return the message for this LogRecord. | |
| Return the message for this LogRecord after merging any user-supplied | |
| arguments with the message. | |
| """ | |
| msg = str(self.msg) | |
| if self.args: | |
| msg = msg % self.args | |
| return msg |
cpython/Lib/logging/__init__.py
Line 711 in 0dcbc83
| record.message = record.getMessage() |
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
charfadolfinus
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Projects
Status
No status