Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging 404 Not Found pages to Sentry with Django

I'd like to log 404 Not Found errors codes to Sentry in Django 1.7.

What hooks Django provides so I could push logger.error() message for these?

Any other ideas how one should monitor non-500 missing / weirdly behaving pages with Django and Sentry?

like image 602
Mikko Ohtamaa Avatar asked Jan 26 '15 22:01

Mikko Ohtamaa


People also ask

How do I fix Django error page not found?

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. Let's do that by turning off Django debug mode. For this, we need to update the settings.py file.

What is Sentry used for in Django?

Sentry's Django integration enables automatic reporting of errors and exceptions. Sentry's Django integration reports all exceptions leading to an Internal Server Error and creates a separate scope for each request. The integration supports Django Web Framework from Version 1.6 and above .


1 Answers

Checkout the Reporting other status codes section of the Sentry Docs for Django:

If you want to report 404 Not Found and other errors besides uncaught exceptions (500 Internal Server Error) to Sentry, you need to write your own Django view for those status codes. For example:

# in the root URL conf urls.py

handler404 = 'myapp.views.my_custom_page_not_found_view'

# myapp/views.py

from django.http import HttpResponseNotFound
from sentry_sdk import capture_message


def my_custom_page_not_found_view(*args, **kwargs):
    capture_message("Page not found!", level="error")

    # return any response here, e.g.:
    return HttpResponseNotFound("Not found")

For more information on customizing errors, read the relevant Django Docs

like image 136
GunnerFan Avatar answered Dec 01 '22 18:12

GunnerFan