8000 ref(logging): Clarify separate warnings case is for Python <3.11 by szokeasaurusrex · Pull Request #4296 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

ref(logging): Clarify separate warnings case is for Python <3.11 #4296

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
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
39 changes: 19 additions & 20 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys
from datetime import datetime, timezone
from fnmatch import fnmatch

Expand Down Expand Up @@ -248,27 +249,25 @@ def _emit(self, record):
event["level"] = level # type: ignore[typeddict-item]
event["logger"] = record.name

# Log records from `warnings` module as separate issues
record_captured_from_warnings_module = (
record.name == "py.warnings" and record.msg == "%s"
)
if record_captured_from_warnings_module:
# use the actual message and not "%s" as the message
# this prevents grouping all warnings under one "%s" issue
msg = record.args[0] # type: ignore

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

if (
sys.version_info < (3, 11)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this additional version check does not change actual behavior. In Python 3.11+, record.msg contains the actual warning message, so the later on record.msg == "%s" check would fail. Adding this version check just makes it more obvious that this code path only exists for older Python versions

and record.name == "py.warnings"
and record.msg == "%s"
):
# warnings module on Python 3.10 and below sets record.msg to "%s"
# and record.args[0] to the actual warning message.
# This was fixed in https://github.com/python/cpython/pull/30975.
message = record.args[0]
params = ()
else:
event["logentry"] = {
"formatted": record.getMessage(),
"message": to_string(record.msg),
"params": record.args,
}
message = record.msg
params = record.args

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

event["extra"] = self._extra_from_record(record)

Expand Down
Loading
0