From 74f559080d357bfb6c6da65a7b2accef6c9e1d82 Mon Sep 17 00:00:00 2001 From: yoshi-lyosha Date: Thu, 23 Apr 2020 19:52:22 +0300 Subject: [PATCH] refactor: change event exc_info default to false avoid binding exception info unless logger.exception or logger.err with exc_info=True is used --- structlog_sentry/__init__.py | 4 ++-- test/test_sentry_processor.py | 18 ++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/structlog_sentry/__init__.py b/structlog_sentry/__init__.py index 28d2cdd..eceec11 100644 --- a/structlog_sentry/__init__.py +++ b/structlog_sentry/__init__.py @@ -39,9 +39,9 @@ def _get_event_and_hint(self, event_dict: dict) -> Tuple[dict, Optional[str]]: :param event_dict: structlog event_dict """ - exc_info = event_dict.get("exc_info", True) + exc_info = event_dict.get("exc_info", False) if exc_info is True: - # logger.exeception() or logger.error(exc_info=True) + # logger.exception() or logger.error(exc_info=True) exc_info = sys.exc_info() has_exc_info = exc_info and exc_info != (None, None, None) diff --git a/test/test_sentry_processor.py b/test/test_sentry_processor.py index 8131646..d94fade 100644 --- a/test/test_sentry_processor.py +++ b/test/test_sentry_processor.py @@ -52,11 +52,9 @@ def test_sentry_log(mocker, level): @pytest.mark.parametrize("level", ["error", "critical"]) def test_sentry_log_failure(mocker, level): + """Make sure that events without exc_info=True will have no + 'exception' information after processing""" m_capture_event = mocker.patch("structlog_sentry.capture_event") - mocker.patch( - "structlog_sentry.event_from_exception", - return_value=({"exception": mocker.sentinel.exception}, mocker.sentinel.hint), - ) event_data = {"level": level, "event": level + " message"} sentry_event_data = event_data.copy() @@ -67,13 +65,8 @@ def test_sentry_log_failure(mocker, level): processor(None, None, event_data) m_capture_event.assert_called_once_with( - { - "level": level, - "message": event_data["event"], - "exception": mocker.sentinel.exception, - "extra": sentry_event_data, - }, - hint=mocker.sentinel.hint, + {"level": level, "message": event_data["event"], "extra": sentry_event_data}, + hint=None, ) @@ -94,8 +87,9 @@ def test_sentry_log_failure_exc_info_true(mocker, level): processor(None, None, event_data) assert m_capture_event.call_count == 1 - _, kwargs = m_capture_event.call_args + args, kwargs = m_capture_event.call_args assert kwargs["hint"]["exc_info"][0] == ZeroDivisionError + assert "exception" in args[0] absent = object()