1

Using Django, I need to do some per-request logging that involves database writes.

I understand Django's process_request() and process_response() middleware hooks, but as far as I can tell, those hooks are in the critical path (by design) for rendering the Web page response.

I'd prefer not to have my post-request database write operations holding up the response time for the page.

Is there a simple design pattern for Django that lets me do a "lazy log write", where I can use request hooks to gather information during request processing, but any follow-on operations and the actual log write operation does not occur until after the response is written to the user?

I'm using WSGI currently, but would prefer the most general solution possible.

payne
  • 13,833
  • 5
  • 42
  • 49

1 Answers1

5

Django implements a request_finished-signal which is fired after the processing of the response has been finished, but the down side is, it will not allow you to access the current request object which makes it not quite useful for logging... The latest place to hook into django's response processing is most probably in the HttpResponse class itself. You could eg. store the data temporarily in request.session and write them to the database in the close() method.

But I guess there are also other alternatives you should consider: You could use something like Celery to handle your logging tasks asynchronously. Furthermore there are non-sql databases like MongoDB that offer you handy and performant features for logging, eg. you dont have to wait until the changes are really committed to the database which can give you a big performance advantage.

Community
  • 1
  • 1
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • 1
    Signals are actually executed in-process, but +1 for Celery as an asynchronous solution. – Daniel Roseman Mar 16 '11 at 21:29
  • Well you're right, looked it up in the code base, the signal is sent before the response is returned by the WSGI handler, though the signal's name might suggest something else. For everybody interested: http://code.djangoproject.com/browser/django/trunk/django/core/handlers/wsgi.py#L275 – Bernhard Vallant Mar 16 '11 at 21:44