getsentry / raven-python

Raven is the legacy Python client for Sentry (getsentry.com) — replaced by sentry-python
https://sentry.io
BSD 3-Clause "New" or "Revised" License
1.68k stars 657 forks source link

Catching unhandled exceptions in Python #1295

Closed daghemo closed 6 years ago

daghemo commented 6 years ago

Hi there, is there any option to catch unhandled exceptions from within a Python program (no Flask/Django)?

I can do this in Azure Application Insights as follows:

#!/bin/env python
import applicationinsights.exceptions
applicationinsights.exceptions.enable( '********-****-****-****-************' )
print 2/0

The above code would report a ZeroDivisionError exeception to the Azure Application Insights console.

Regards, Danilo

lingfish commented 6 years ago

I'd like to know this too. Just started using Sentry, and thought this was exactly the point of it, to catch unhandled exceptions in stock Python so that devs didn't have to go hunting through logs.

A big fat except: seems heavyhanded.

mattrobenolt commented 6 years ago

By default, we instrument sys.excepthook once you initialize a Client object. In which case, then completely uncaught exceptions would be recorded.

lingfish commented 6 years ago

Ah, very nice! I kinda hoped so, it just isn't super clear in the doco. Not sure if that is able to be contributed to or not? Otherwise, perhaps a suggestion for it to be added ;)

daghemo commented 6 years ago

Tested and confirmed. Maybe some more sleep needed here: I was convinced I had already tried!

mitsuhiko commented 6 years ago

The new SDK handles this out of the box automatically. Have a look at sentry-python.