Skip to content

Commit 33d48a4

Browse files
committed
Always use LogRecord.getMessage to get the log body
Mostly a revert of open-telemetry#3343
1 parent 415c94f commit 33d48a4

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
([#4260](https://github.com/open-telemetry/opentelemetry-python/pull/4260))
2828
- semantic-conventions: Bump to 1.29.0
2929
([#4337](https://github.com/open-telemetry/opentelemetry-python/pull/4337))
30+
- Further improve compatibility with other logging libraries that override
31+
`LogRecord.getMessage()` in order to customize message formatting
32+
([#4327](https://github.com/open-telemetry/opentelemetry-python/pull/4327))
3033

3134
## Version 1.28.0/0.49b0 (2024-11-05)
3235

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: 5 additions & 8 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)
@@ -232,8 +232,7 @@ def test_simple_log_record_processor_different_msg_types(self):
232232

233233
def test_simple_log_record_processor_custom_single_obj(self):
234234
"""
235-
Tests that special-case handling for logging a single non-string object
236-
is correctly applied.
235+
Tests that logging a single non-string object uses getMessage
237236
"""
238237
exporter = InMemoryLogExporter()
239238
log_record_processor = BatchLogRecordProcessor(exporter)
@@ -259,9 +258,7 @@ def test_simple_log_record_processor_custom_single_obj(self):
259258
logger.warning("a string with a percent-s: %s", "and arg")
260259
# non-string msg with args - getMessage stringifies msg and formats args into it
261260
logger.warning(["a non-string with a percent-s", "%s"], "and arg")
262-
# non-string msg with no args:
263-
# - normally getMessage would stringify the object and bypass formatting
264-
# - SPECIAL CASE: bypass stringification as well to keep the raw object
261+
# non-string msg with no args - getMessage stringifies the object and bypasses formatting
265262
logger.warning(["a non-string with a percent-s", "%s"])
266263
log_record_processor.shutdown()
267264

@@ -270,7 +267,7 @@ def test_simple_log_record_processor_custom_single_obj(self):
270267
("a string with a percent-s: %s"),
271268
("a string with a percent-s: and arg"),
272269
("['a non-string with a percent-s', 'and arg']"),
273-
(["a non-string with a percent-s", "%s"]),
270+
("['a non-string with a percent-s', '%s']"),
274271
]
275272
for emitted, expected in zip(finished_logs, expected):
276273
self.assertEqual(emitted.log_record.body, expected)

0 commit comments

Comments
 (0)