Skip to content

Commit

Permalink
refactor: change event exc_info default to false
Browse files Browse the repository at this point in the history
avoid binding exception info unless logger.exception or logger.err with exc_info=True is used
  • Loading branch information
yoshi-lyosha authored and aexvir committed Jan 26, 2021
1 parent 2de53b9 commit 74f5590
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
4 changes: 2 additions & 2 deletions structlog_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 6 additions & 12 deletions test/test_sentry_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
)


Expand All @@ -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()
Expand Down

0 comments on commit 74f5590

Please sign in to comment.