Suppress log_response() logging for short periods.

I have a regular pattern; where I want to log a bit more detail on an issue - but show the user not much more than an opaque ‘Not Found’ (and certainly not reflect entered values back).

E.g.

def view_foobar(request, node=None, tag=None):
    try:
         machines = Machine.objects.filter(something...
    except ObjectDoesNotExist:
        logger.error("Node {} not found .....
        return HttpResponse("Not found", status=404, content_type="text/plain")

Which then in the logs yields something such as

 2025-01-04 10:23:34,068 [ERROR] acl.views: Node XXX not found....
 2025-01-04 10:23:34,072 [WARNING] django.request: Not Found: /acl/api/v1/getok/spacedeur

With the second Warning coming from BaseHandler (django/core/handlers/base.py) its get_response() method that calls log_response().

How does one best suppress this second warning / overwrite that method narrowly for just cases like this. But still keep the ability to do rich/complex HttpResponse() and so on / not recreate most of HttpResponse (or raise NotFound; etc) ? And without fiddling with the log level ideally ?

You could create a custom Middleware that suppresses the warning log for specific views or conditions.

Welcome @dirkx !

In a proper deployment environment (with DEBUG=False), you shouldn’t get any kind of detailed information in the response. What you’re looking for is how it works normally.

If you are seeing anything else, please provide more details about the situation, including a description of the environment in which you’re running this and screenshots of the improper response coming from the view.

Thanks - that is sort of what I feared & have since put in with an extra sentinel to trigger this just in those cases. Thanks.

1 Like