Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

feat: add extra={'stack': True} support for logbook handler #1228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/integrations/logbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ Finally, bind your handler to your context::
with sentry_handler.applicationbound():
# everything logged here will go to sentry.
...

You can also use the ``extra={'stack': True}`` arguments on
your ``log`` methods. This will store the appropriate information and allow
Sentry to render it based on that information::

# If you don't have an exception, but still want to capture a
# stacktrace, use the `stack` arg
logger.error('There was an error, with a stacktrace!', extra={
'stack': True,
})

2 changes: 2 additions & 0 deletions raven/handlers/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def _emit(self, record):

if 'tags' in record.kwargs:
handler_kwargs['tags'] = record.kwargs['tags']
if 'stack' in record.extra:
handler_kwargs['stack'] = record.extra['stack']

# If there's no exception being processed, exc_info may be a 3-tuple of None
# http://docs.python.org/library/sys.html#sys.exc_info
Expand Down
16 changes: 16 additions & 0 deletions tests/handlers/logbook/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def test_logger(self):
self.assertEquals(msg['message'], 'This is a test info with a url')
self.assertEquals(msg['params'], ())

logger.error('This is a test of stacks', extra=dict(
stack=True
))
self.assertEquals(len(client.events), 1)
event = client.events.pop(0)
self.assertTrue(event['extra']['stack'])
self.assertFalse('exception' in event)
self.assertTrue('sentry.interfaces.Message' in event)
msg = event['sentry.interfaces.Message']
self.assertTrue('stacktrace' in event)
frames = event['stacktrace']['frames']
self.assertNotEquals(len(frames), 1)
frame = frames[0]
self.assertEquals(msg['message'], 'This is a test of stacks')
self.assertEquals(msg['params'], ())

try:
raise ValueError('This is a test ValueError')
except ValueError:
Expand Down