From 7c7ab8312894993cbaecdcb0ea8596117e41bb1d Mon Sep 17 00:00:00 2001 From: Guilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:52:21 -0300 Subject: [PATCH] add capability for the logging integration to use the parameter 'stack_info' (added in Python 3.2). When set to True the stack trace will be retrieved and properly handled --- sentry_sdk/integrations/logging.py | 2 +- tests/integrations/logging/test_logging.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 5d23440ad1..b792510d6c 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -202,7 +202,7 @@ def _emit(self, record): client_options=client_options, mechanism={"type": "logging", "handled": True}, ) - elif record.exc_info and record.exc_info[0] is None: + elif (record.exc_info and record.exc_info[0] is None) or record.stack_info: event = {} hint = {} with capture_internal_exceptions(): diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 02eb26a04d..f880c841d7 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -77,11 +77,14 @@ def test_logging_extra_data_integer_keys(sentry_init, capture_events): assert event["extra"] == {"1": 1} -def test_logging_stack(sentry_init, capture_events): +@pytest.mark.parametrize( + "enable_stack_trace_kwarg", ({"exc_info": True}, {"stack_info": True}) +) +def test_logging_stack_trace(sentry_init, capture_events, enable_stack_kwarg): sentry_init(integrations=[LoggingIntegration()], default_integrations=False) events = capture_events() - logger.error("first", exc_info=True) + logger.error("first", **enable_stack_kwarg) logger.error("second") (