title: How can I catch all 500 and above errors in DRF and log them?
body: <p>I've looked through much of the SO responses regarding implementing this but I can't seem to find something that isn't deprecated or one that actually works for my use case.</p>
  <p>Basically, I have some API methods that return their own status codes. However, if for any reason there is a 500 Server Error or anything above 500 which indicates something strange happened (for example lets say the database connection got timeout), I want that to be logged to a file or e-mailed to an admin.</p>
  <p>I have tried using a custom exception handler but not all 500 errors are exceptions.</p>
  <p>So I resorted to writing custom middleware, which looked something like this</p>
  <pre><code>class CustomApiLoggingMiddleware(MiddlewareMixin):
      def process_response(self, request, response):
          if response.path.startswith('/api/'):
              ...do stuff here
  </code></pre>
  <p>The issue, however, is that there doesn't seem to be a <code>status_code</code>. To be frank I don't really know which response I'm actually getting. There's the 500 response returned by my API intentionally as a result of the API method, and there's the 500 response generated by Django in the backend if some kind of condition isn't met (let's say they sent a request without a proper header)</p>
  <p>I tried to generate a 500 response / error intentionally by returning <code>Response(status=status.HTTP_500)</code> in some basic <code>/500</code> route. Even though this triggers the middleware, there is no <code>status_code</code> that I can use to check whether the response is actually a 500 or not.</p>
  <p>I'm not even sure I'm doing this right, but hopefully you understand my intention. I would basically like to blanket catch all uncaught exceptions that can happen as a result of my API calls and log them or send them to an admin. Most calls that return some 40X or whatever are handled in the way that I want already.</p>