我想用Sentry + Raven检测独立Python脚本中的错误
我尝试配置它,raven test ...
正在工作
然后我把它放在脚本之上:
from raven import Client
client = Client('http://...@.../1')
client.captureException()
稍后会生成异常:
import django
django.setup()
from django.conf import settings
我希望看到这个错误的实际堆栈:
ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'settings'
但我在Sentry看到的只是
这完全没用。
如何更改此设置以获得正常的追溯?
答案 0 :(得分:3)
您误解了client.captureException()
的工作原理,而不是配置参数。在捕获异常时使用它,它将捕获异常类型和消息:
try:
f = open('oogah-boogah.txt')
except IOError:
client.captureException()
# do something here
要捕获可能在代码块中生成的任何异常,您可以使用capture_exceptions
:
@client.capture_exceptions
def load_django():
import django
django.setup()
from django.conf import settings
是的,你是对的,但有没有办法去捕捉异常 在try-except中包装一段代码。我可以看到错误 终端,我可以在Sentry中看到它吗?
有一个默认的异常处理程序 - 当没有捕获到异常时,这个默认处理程序捕获它然后显示异常。这就是你在终端上看到的。
生成此输出的函数为sys.excepthook
,默认情况下会输出到stderr
。
因此,为了全局捕获所有异常,您必须创建一个全局异常处理程序或将您自己的函数映射到sys.excepthook
。
我强烈建议不要这样做,但因为你不知道它可能有什么副作用。