From caa7c3a0d6e6298a80fc446101ee6609e9e8152d Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 15 Apr 2025 10:31:37 +0200 Subject: [PATCH] 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 --- sentry_sdk/integrations/logging.py | 39 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index ec13c86c6e..bf538ac7c7 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -1,4 +1,5 @@ import logging +import sys from datetime import datetime, timezone from fnmatch import fnmatch @@ -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) + 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)