7

raven 有很多集成,包括 python 日志记录。一方面,twisted 不使用 python 的日志记录。而另一方面,raven 在twisted 中并没有直接的整合。

那么,在基于扭曲的设置中使用 raven 的当前最佳实践是什么?

4

3 回答 3

11

ravencaptureException如果有异常活动,则只能在没有参数的情况下调用's ,而调用日志观察器时并非总是如此。因此,取而代之的是,将异常信息从Failure记录的内容中提取出来:

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)
于 2014-06-19T18:15:04.807 回答
3

user1252307 的回答是一个很好的开始,但是在哨兵方面,您会得到一个相对无用的字典并且没有堆栈跟踪。

如果您尝试查看和跟踪意外异常,请尝试对 log_sentry 函数进行以下小改动:

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)

可能有更好的方法来过滤基于非异常的错误消息,这可能会尝试发出不存在的异常信息,其中存在非异常的故障。

于 2014-05-14T17:21:18.260 回答
1
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)
于 2014-03-17T03:25:19.087 回答