[Django]-`django.request` logger not logging 4xx and 5xx responses from views

4πŸ‘

βœ…

I ran into this issue myself some time ago. Here is what happens:

  • 500 errors are only logged if they are the result of an uncaught exception. If you set a 500 status manually then it will not be logged. Here’s the relevant part of the code – as you can see that piece of logging happens inside a handle_uncaught_exception method.

  • The behaviour for 404 responses is currently the same – only uncaught Http404 responses are logged. However, this has changed in Django 1.10. See my comment on this ticket:

    I just want to note that this change has altered the behaviour of Django’s 404 logging somewhat.

    Previously, Django would only log a 404 if a Http404 exception bubbled up to the core handler (and not if a view/middleware caught the exception and returned a response, regardless of whether that response was still a 404).

    Now, it will log all 404 responses regardless of how they were generated. This seems better to me but there may be applications that were relying on the previous behaviour.

    So from Django 1.10 onwards all 404 responses will get logged. It seems a bit inconsistent to me.

  • Other 4xx responses are only logged if the corresponding exception is raised. 400 exceptions are logged to django.security and not to django.request.

To answer your other question – what is the best way to consistently log this stuff? – the most reliable approach I have found is to write your own middleware which checks the status of all responses and does its own logging.

Edit: I’ve created a ticket that tries to address some of these inconsistencies in Django. The issue is fixed in Django 2.1.

πŸ‘€solarissmoke

-1πŸ‘

If you only need to see the errors and you are using apache2.4 server, you can find it at /var/log/apache2/error.log

The access log can be found at /var/log/apache2/access.log

πŸ‘€sprksh

Leave a comment