Skip to content

Commit 0501ee7

Browse files
ref(logging): Clarify separate warnings case is for Python <3.11
The way the code was written before this change made it look like log records from the `warnings` module were always being handled by a separate code path. In fact, this separate path is only used for Python 3.10 and below. This change makes it clear that the branch is version specific. That way, when we eventually stop supporting 3.10, it is clear that we can delete this separate block. Depends on: - #4292 - #4291
1 parent 296e288 commit 0501ee7

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

sentry_sdk/integrations/logging.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import sys
23
from datetime import datetime, timezone
34
from fnmatch import fnmatch
45

@@ -248,27 +249,26 @@ def _emit(self, record):
248249
event["level"] = level # type: ignore[typeddict-item]
249250
event["logger"] = record.name
250251

251-
# Log records from `warnings` module as separate issues
252-
record_captured_from_warnings_module = (
253-
record.name == "py.warnings" and record.msg == "%s"
254-
)
255-
if record_captured_from_warnings_module:
256-
# use the actual message and not "%s" as the message
257-
# this prevents grouping all warnings under one "%s" issue
258-
msg = record.args[0] # type: ignore
259-
260-
event["logentry"] = {
261-
"message": msg,
262-
"formatted": record.getMessage(),
263-
"params": (),
264-
}
265-
252+
if (
253+
sys.version_info < (3, 11)
254+
and record.name == "py.warnings"
255+
and record.msg == "%s"
256+
and len(record.args) == 1
257+
):
258+
# warnings module on Python 3.10 and below sets record.msg to "%s"
259+
# and record.args[0] to the actual warning message.
260+
# This was fixed in https://github.com/python/cpython/pull/30975.
261+
message = record.args[0]
262+
params = ()
266263
else:
267-
event["logentry"] = {
268-
"formatted": record.getMessage(),
269-
"message": to_string(record.msg),
270-
"params": record.args,
271-
}
264+
message = record.msg
265+
params = record.args
266+
267+
event["logentry"] = {
268+
"message": to_string(message),
269+
"formatted": record.getMessage(),
270+
"params": params,
271+
}
272272

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

0 commit comments

Comments
 (0)