diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index a677d0c507..cb0896bbcc 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -395,7 +395,6 @@ def _can_record(self, record: "LogRecord") -> bool: def emit(self, record: "LogRecord") -> "Any": with capture_internal_exceptions(): - self.format(record) if not self._can_record(record): return @@ -420,6 +419,7 @@ def emit(self, record: "LogRecord") -> "Any": if not should_capture_logs: return + self.format(record) self._capture_log_from_record(client, record) def _capture_log_from_record( diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 661fc53f57..15ceb8dd65 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -8,6 +8,7 @@ from sentry_sdk.consts import VERSION from sentry_sdk.integrations.logging import ( LoggingIntegration, + SentryLogsHandler, ignore_logger, ignore_logger_for_sentry_logs, unignore_logger, @@ -244,6 +245,23 @@ def test_sentry_logs_collection_off_by_default(sentry_init, capture_items, reque assert not items +def test_sentry_logs_handler_skips_format_when_disabled(sentry_init): + sentry_init(integrations=[LoggingIntegration(capture_sentry_logs=False)]) + handler = SentryLogsHandler() + record = logging.LogRecord( + name="test-logger", + level=logging.INFO, + pathname=__file__, + lineno=1, + msg="hello %s", + args=("world",), + exc_info=None, + ) + with mock.patch.object(handler, "format") as formatted: + handler.emit(record) + formatted.assert_not_called() + + def test_sentry_logs_collection_opt_in(sentry_init, capture_items, request): """Automatic logs capture by Sentry logs needs explicit opt-in via capture_sentry_logs.""" sentry_init(integrations=[LoggingIntegration(capture_sentry_logs=True)])