Search code examples
djangosentryraven

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?


Solution

  • 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