Skip to content

Commit 4658128

Browse files
committed
Always use LogRecord.getMessage to get the log body
Mostly a revert of open-telemetry#3343
1 parent 2ddb076 commit 4658128

File tree

2 files changed

+3
-18
lines changed

2 files changed

+3
-18
lines changed

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
510510
if self.formatter:
511511
body = self.format(record)
512512
else:
513-
# `record.getMessage()` uses `record.msg` as a template to format
514-
# `record.args` into. There is a special case in `record.getMessage()`
515-
# where it will only attempt formatting if args are provided,
516-
# otherwise, it just stringifies `record.msg`.
517-
#
518-
# Since the OTLP body field has a type of 'any' and the logging module
519-
# is sometimes used in such a way that objects incorrectly end up
520-
# set as record.msg, in those cases we would like to bypass
521-
# `record.getMessage()` completely and set the body to the object
522-
# itself instead of its string representation.
523-
# For more background, see: https://github.com/open-telemetry/opentelemetry-python/pull/4216
524-
if not record.args and not isinstance(record.msg, str):
525-
# no args are provided so it's *mostly* safe to use the message template as the body
526-
body = record.msg
527-
else:
528-
body = record.getMessage()
513+
body = record.getMessage()
529514

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

opentelemetry-sdk/tests/logs/test_export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def test_simple_log_record_processor_different_msg_types(self):
217217
"Temperature hits high 420 C in Hyderabad",
218218
"CRITICAL",
219219
),
220-
(["list", "of", "strings"], "WARN"),
221-
({"key": "value"}, "ERROR"),
220+
("['list', 'of', 'strings']", "WARN"),
221+
("{'key': 'value'}", "ERROR"),
222222
]
223223
emitted = [
224224
(item.log_record.body, item.log_record.severity_text)

0 commit comments

Comments
 (0)