Skip to content

Instantly share code, notes, and snippets.

@bobuss
Created March 5, 2014 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobuss/9366148 to your computer and use it in GitHub Desktop.
Save bobuss/9366148 to your computer and use it in GitHub Desktop.
Send locals to your sentry server while an exception occurs
from __future__ import absolute_import
import pprint
from raven.handlers.logging import SentryHandler
class VerboseSentryHandler(SentryHandler, object):
"""
Subclass the SentryHandler in order to add the locals to the record
Inspired by The Verbose exception Formatter
From Bitly blog : http://word.bitly.com/post/69080588278/logging-locals
"""
def _emit(self, record, **kwargs):
tb = record.exc_info[2] # This is the outermost frame of the traceback.
while tb.tb_next:
tb = tb.tb_next # Zoom to the innermost frame.
record.extra = {
'data': {k: pprint.pformat(v) for k, v in tb.tb_frame.f_locals.iteritems()}}
super(VerboseSentryHandler, self)._emit(record, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment