Question

There are many integrations for raven, including python logging. On the one side, twisted does not use python's logging. And on the other side, there is no direct integration for raven in twisted.

So what is the current best practice for using raven in a twisted based setup?

Was it helpful?

Solution

raven's captureException can only be called without arguments if there is an exception active, which is not always the case when a log observer is called. So, instead, pull the exception information out of the Failure that gets logged:

from twisted.python import log
from raven import Client


client = Client(dsn='twisted+http://YOUR_DSN_HERE')

def logToSentry(event):
    if not event.get('isError') or 'failure' not in event:
        return

    f = event['failure']
    client.captureException((f.type, f.value, f.getTracebackObject()))

log.addObserver(logToSentry)

OTHER TIPS

user1252307 answer is a great start, but on the sentry side you get a relatively unhelpful dictionary and no stack trace.

If you are trying to view and track down unexpected exceptions try this small change to the log_sentry function:

from twisted.python import log
from raven import Client

client = Client(dsn='twisted+http://YOUR_DSN_HERE')

def log_sentry(dictionary):
    if dictionary.get('isError'):
        if 'failure' in dictionary:
            client.captureException() # Send the current exception info to Sentry.
        else:
            #format the dictionary in whatever way you want
            client.captureMessage(dictionary)

log.addObserver(log_sentry)

There maybe a better way to filter non exception based error message, and this might try to emit exception information that doesn't exist where there are failures which aren't exceptions.

from twisted.python import log
from raven import Client

client = Client(dsn='twisted+http://YOUR_DSN_HERE')

def log_sentry(dictionary):
    if dictionary.get('isError'):
        #format the dictionary in whatever way you want
        client.captureMessage(dictionary)

log.addObserver(log_sentry)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top