Fixing the Biggest Issue with Django Sites Hosted on Heroku

Series: Learning Django
Published: Sep 27, 2021
Updated: Sep 28, 2021

Hey y'all!

Today's post is a short one, as it mostly pertains to fixing a rather rudimentary issue of Django sites hosted on Herkou: error logging. While it's possible to store your errors in a log file, those can be pesky and cumbersome to work with when using Heroku. Heroku's default logs are also not helpful in many situations, as they deal with higher level server errors and don't necessarily show you the full error stack. To get around this issue, this site is now officially connected to the Sentry SDK! By connecting the application's logging system to Sentry, I have full access to the following:

  1. Full error stack messages when something breaks.
  2. All transaction attempts on all endpoints of my site.

The first point is useful for obvious reasons. In fact, it has already helped me capture a few minor errors that ocurred when users clicked on certain 'Categories' that broke the query logic associated with fetching that pages information.

The second point above has proven insightful moreso than helpful, as it has shown me all of the bots trying to parse my website for information. It reminded me that one thing I neglected when creating the site is a robots.txt file! It also reminded me to finally add a favicon to my site. The implementation was incredibly easy, and if you're facing similar issues debugging with your Django sites on Heroku, I highly recommend adding Sentry to your project. It takes about 4 lines of code, and you're good to go!

To add Sentry to your site, I'd recommend reading their latest documentation here, but for illustrative purposes, here's all it took:

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration


    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    # We recommend adjusting this value in production,

    # If you wish to associate users to errors (assuming you are using
    # django.contrib.auth) you may enable sending PII data.

    # By default the SDK will try to use the SENTRY_RELEASE
    # environment variable, or infer a git commit
    # SHA as release, however you may want to set
    # something more human-readable.
    # release="myapp@1.0.0",