0

I have a django project on production. In my settings.py file Debug=False to avoid security risks which arrises when Debug=True I heard about python logging module which I am trying to set up. But after reading documentation like I understand I should put logging instances in each piece of code where error can be arrised, but what if I need to handle 404 errors? How I can handle them? Please anyone guide me

1 Answers1

1

I think there is quite a bunch of information around about 404 handling in django. The most basic thing might be the 404 view that you can override with your custom logics (which i think is not preferred, but might come in handy in some specific cases): https://docs.djangoproject.com/en/3.0/topics/http/views/#customizing-error-views

The preferred method would be going deeper in to the core of request and response handling where you can manipulate your requests having a response status_code of 404 or what so ever. In your middleware you can extend and override behavior of existing middlewares or add your own: https://docs.djangoproject.com/en/3.0/topics/http/middleware/

Below you have simple example of a middleware that checkes the response's status code and uses log.warning when the reponse has a status_code of 404. In this scenario you'll have a single location being responsible of logging the faulty paths that are being requested.

# myproject.middleware.py
import logging

logger = logging.getLogger(__name__)


class PageNotFoundMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        if response.status_code == 404:
            logger.warning("Page not found at {}".format(request.path))
        return response

in your settings.py you can now add the middleware:

MIDDLEWARE = [
   ...
   'myproject.middleware.PageNotFoundMiddleware',
]
Nrzonline
  • 1,600
  • 2
  • 18
  • 37
  • I mostly was asking about another to log error traceback to the file. But thank you – Наглый Спамер Feb 16 '20 at 14:14
  • I don't understand your comment... Can you elaborate a bit further on your exact wishes? – Nrzonline Feb 16 '20 at 20:33
  • Your answer is totally covered scenario of handling 404 error. But maybe my question wasn't asked understandably. I was asking about how I can log full traceback like traceback which django handle when Debug=True – Наглый Спамер Feb 16 '20 at 20:43
  • Django' `BaseHandler` will use the `convert_exception_to_response` and `response_for_exception`. This is where django checks if `DEBUG` is enabled and will call the `technical_404_response`. In your case you want to add an exception middleware that catches the 404 and process it in your own modified `technical_404_response` to use logging instead of returning a response with the data you need. – Nrzonline Feb 16 '20 at 21:19
  • Note that there are solutions e.g. Sentry that could handle the logging for you, as well as all other exceptions that occur in your web application. – Nrzonline Feb 16 '20 at 21:28