You've deployed a Django app to a webserver and now it's not working. Your app is throwing 500 Internal Server Errors - what's wrong? Why is this happening? It worked on my laptop!?
Even worse is when a customer experienced an error 12 hours ago and you need to figure out what went wrong.
You need something to alert you when errors happen in production, otherwise you're flying blind. How can you fix a bug if you don't know what happened? Error reporting is important if you're a new developer, because you're going to write a lot of bugs, or if you're experienced, since other people are likely relying on your code to work.
Django allows you to set up email reports, which requires some fiddling with mail servers, but it's a totally OK way to track errors.
My favourite way to monitor errors is using Sentry. It's a SaaS product that's been used at every Django job I've worked at and I use it for my personal projects. Here's why I like it so much.
Sentry used to be a little harder to install, but now there are only 3 things you need to do in order to get started.
Install the Python package
pip install sentry-sdk
Set an environment variable
export SENTRY_DSN="https://[email protected]/yyy"
And run a line of Python in your production settings.py
import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration sentry_sdk.init( dsn=os.environ.get("SENTRY_DSN"), integrations=[DjangoIntegration()], environment="prod" )
You want as much information as possible on the incident:
Sentry captures an incredible amount of info when it logs an error, sometimes including the values of variables in scope and the database queries that were run.
I host all my personal projects in Sentry for free. I think as long as you stick to one user you don't have to pay for it.
Some people might not like this, but if you pefer Slack to email, you can set up Sentry to post to a Slack channel when an error crops up.
If you're willing to do a little more legwork, you can configure Sentry to track your deployments. You give it a Git commit hash and it is able to correlate errors with particular deployments, making it easier to track down the offending code. This is particularly useful if you/your team are shippng multiple deployments per day.
As you might have noticed, I'm pretty happy with Sentry, even after a few years of using it. There are a few little issues with it, like the overly-complex settings panel in the web UI, but overall it offers a low-friction user experience. Hopefully you'll get some use out of it.
Now, just because you have error monitoring set up, that doesn't mean you've done everything you need to in order to monitor your production environment. Application logging is essential as well! If you haven't already set up your Django app to write logs in production you can find out how here.
If you have any feedback or questions email me at [email protected]