Skip to content

Commit 6a71720

Browse files
committed
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.
1 parent 189fa15 commit 6a71720

File tree

1 file changed

+2
-4
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal

1 file changed

+2
-4
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
438438
"exc_text",
439439
"filename",
440440
"funcName",
441+
"getMessage",
441442
"message",
442443
"levelname",
443444
"levelno",
@@ -547,10 +548,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
547548
if self.formatter:
548549
body = self.format(record)
549550
else:
550-
if isinstance(record.msg, str) and record.args:
551-
body = record.msg % record.args
552-
else:
553-
body = record.msg
551+
body = record.getMessage()
554552

555553
# related to https://github.com/open-telemetry/opentelemetry-python/issues/3548
556554
# Severity Text = WARN as defined in https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity.

0 commit comments

Comments
 (0)