[logging] cache formatted log messages

This commit is contained in:
Sam Schott 2021-03-21 15:04:06 +00:00
parent 312196fdb8
commit 0eef290e69

View File

@ -55,12 +55,16 @@ class EncodingSafeLogRecord(logging.LogRecord):
a :class:`UnicodeEncodeError` under many circumstances (printing to stdout, etc).
"""
_msg: Optional[str] = None
def getMessage(self) -> str:
"""
Formats the log message and replaces all surrogate escapes with "<EFBFBD>".
"""
msg = super().getMessage()
return sanitize_string(msg)
if not self._msg:
self._msg = sanitize_string(super().getMessage())
return self._msg
logging.setLogRecordFactory(EncodingSafeLogRecord)