From 39a77eaf5f91132dbd869e6e67fd85718d53163e Mon Sep 17 00:00:00 2001 From: Carey Metcalfe Date: Wed, 9 Oct 2024 09:38:07 -0400 Subject: [PATCH] Use LogRecord.getMessage to get OLTP body This improves compatibility with logging libraries that use `logging.setLogRecordFactory()` or patching to customize the message formatting of the created `LogRecord`s. It does this by using the processed `LogRecord`'s `getMessage()` method to get the body text instead of using `record.msg % record.args`. Also adds "getMessage" to the list of reserved attributes so if the customization is done by patching the `getMessage` function on the `LogRecord` directly (instead of using a subclass), it's not accidentally treated as an attribute. --- .../src/opentelemetry/sdk/_logs/_internal/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index b7e56eda38..d7b7ec8ff2 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -438,6 +438,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool: "exc_text", "filename", "funcName", + "getMessage", "message", "levelname", "levelno", @@ -548,7 +549,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord: body = self.format(record) else: if isinstance(record.msg, str) and record.args: - body = record.msg % record.args + body = record.getMessage() else: body = record.msg