Skip to content

feat(logging): Add formatted message to log events #4292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ def _emit(self, record):

event["logentry"] = {
"message": msg,
"formatted": record.getMessage(),
"params": (),
}

else:
event["logentry"] = {
"formatted": record.getMessage(),
"message": to_string(record.msg),
"params": record.args,
}
Expand Down
19 changes: 19 additions & 0 deletions tests/integrations/logging/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_logging_works_with_many_loggers(sentry_init, capture_events, logger):
assert event["level"] == "fatal"
assert not event["logentry"]["params"]
assert event["logentry"]["message"] == "LOL"
assert event["logentry"]["formatted"] == "LOL"
assert any(crumb["message"] == "bread" for crumb in event["breadcrumbs"]["values"])


Expand Down Expand Up @@ -112,6 +113,7 @@ def test_logging_level(sentry_init, capture_events):
(event,) = events
assert event["level"] == "error"
assert event["logentry"]["message"] == "hi"
assert event["logentry"]["formatted"] == "hi"

del events[:]

Expand Down Expand Up @@ -152,6 +154,7 @@ def test_custom_log_level_names(sentry_init, capture_events):
assert events
assert events[0]["level"] == sentry_level
assert events[0]["logentry"]["message"] == "Trying level %s"
assert events[0]["logentry"]["formatted"] == f"Trying level {logging_level}"
assert events[0]["logentry"]["params"] == [logging_level]

del events[:]
Expand All @@ -177,6 +180,7 @@ def filter(self, record):

(event,) = events
assert event["logentry"]["message"] == "hi"
assert event["logentry"]["formatted"] == "hi"


def test_logging_captured_warnings(sentry_init, capture_events, recwarn):
Expand All @@ -198,10 +202,16 @@ def test_logging_captured_warnings(sentry_init, capture_events, recwarn):
assert events[0]["level"] == "warning"
# Captured warnings start with the path where the warning was raised
assert "UserWarning: first" in events[0]["logentry"]["message"]
assert "UserWarning: first" in events[0]["logentry"]["formatted"]
# For warnings, the message and formatted message are the same
assert events[0]["logentry"]["message"] == events[0]["logentry"]["formatted"]
assert events[0]["logentry"]["params"] == []

assert events[1]["level"] == "warning"
assert "UserWarning: second" in events[1]["logentry"]["message"]
assert "UserWarning: second" in events[1]["logentry"]["formatted"]
# For warnings, the message and formatted message are the same
assert events[1]["logentry"]["message"] == events[1]["logentry"]["formatted"]
assert events[1]["logentry"]["params"] == []

# Using recwarn suppresses the "third" warning in the test output
Expand Down Expand Up @@ -234,6 +244,7 @@ def test_ignore_logger_wildcard(sentry_init, capture_events):

(event,) = events
assert event["logentry"]["message"] == "hi"
assert event["logentry"]["formatted"] == "hi"


def test_logging_dictionary_interpolation(sentry_init, capture_events):
Expand All @@ -245,6 +256,10 @@ def test_logging_dictionary_interpolation(sentry_init, capture_events):

(event,) = events
assert event["logentry"]["message"] == "this is a log with a dictionary %s"
assert (
event["logentry"]["formatted"]
== "this is a log with a dictionary {'foo': 'bar'}"
)
assert event["logentry"]["params"] == {"foo": "bar"}


Expand All @@ -263,4 +278,8 @@ def test_logging_dictionary_args(sentry_init, capture_events):
event["logentry"]["message"]
== "the value of foo is %(foo)s, and the value of bar is %(bar)s"
)
assert (
event["logentry"]["formatted"]
== "the value of foo is bar, and the value of bar is baz"
)
assert event["logentry"]["params"] == {"foo": "bar", "bar": "baz"}
Loading