Skip to content

Commit caa7c3a

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 caa7c3a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

sentry_sdk/integrations/logging.py

+19-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,25 @@ 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+
):
257+
# warnings module on Python 3.10 and below sets record.msg to "%s"
258+
# and record.args[0] to the actual warning message.
259+
# This was fixed in https://github.com/python/cpython/pull/30975.
260+
message = record.args[0]
261+
params = ()
266262
else:
267-
event["logentry"] = {
268-
"formatted": record.getMessage(),
269-
"message": to_string(record.msg),
270-
"params": record.args,
271-
}
263+
message = record.msg
264+
params = record.args
265+
266+
event["logentry"] = {
267+
"message": to_string(message),
268+
"formatted": record.getMessage(),
269+
"params": params,
270+
}
272271

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

0 commit comments

Comments
 (0)