Search code examples
pythonloggingsentryraven

How to log full call-stack with context in raven/sentry?


When raised exception is caught on the root of call-stack I can see the whole context at every level of call-stack in Sentry.

But, when I use captureMessage() I can't see any context in Sentry.

If I use captureException() as in the code below I can see only the top of call-stack.

try:
    raise Exception('Breakpoint!')
except:
    raven_client.captureException()

In other words I want to see in Sentry a logged message with full stacktrace and context.


Solution

  • The Python SDK has the ability to capture arbitrary stacktraces by passing stack=True to captureMessage:

    raven_client.captureMessage('hello world', stack=True)
    

    There is additionally an auto_log_stacks value that can be turned on when configuring the Client:

    raven_client = Client(..., auto_log_stacks=True)
    

    Caveat: Automatically logging stacks is useful, but it's not guaranteed accurate in some common situations. It's also a performance hit, albeit a minor one, as it has to constantly call out to inspect.stack().