From 0eef290e690c3fd45891fed62a7b2dd60a2ef216 Mon Sep 17 00:00:00 2001 From: Sam Schott Date: Sun, 21 Mar 2021 15:04:06 +0000 Subject: [PATCH] [logging] cache formatted log messages --- src/maestral/logging.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/maestral/logging.py b/src/maestral/logging.py index 854f12ec..c82d5dcb 100644 --- a/src/maestral/logging.py +++ b/src/maestral/logging.py @@ -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 "�". """ - msg = super().getMessage() - return sanitize_string(msg) + if not self._msg: + self._msg = sanitize_string(super().getMessage()) + + return self._msg logging.setLogRecordFactory(EncodingSafeLogRecord)