From 6a717209565e6f246876322a26ec6561a4995211 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 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index b7e56eda38..37aeb7b92f 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", @@ -547,10 +548,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord: if self.formatter: body = self.format(record) else: - if isinstance(record.msg, str) and record.args: - body = record.msg % record.args - else: - body = record.msg + body = record.getMessage() # related to https://github.com/open-telemetry/opentelemetry-python/issues/3548 # Severity Text = WARN as defined in https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity.