Skip to content

Commit

Permalink
Use LogRecord.getMessage to get OLTP body
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pR0Ps committed Oct 9, 2024
1 parent 189fa15 commit 6a71720
Showing 1 changed file with 2 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
"exc_text",
"filename",
"funcName",
"getMessage",
"message",
"levelname",
"levelno",
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6a71720

Please sign in to comment.