[Answered ]-Tracing requests of users by logging their actions to DB in django


I wouldn’t do that. If this is a production service then you’ve got a proper web server running in front of it, right? Apache, or nginx or something. That can do logging, and can do it well, and can write to a form that won’t bloat your database, and there’s a wealth of analytical tools for log analysis.

You are going to have to duplicate a lot of that functionality in your decorator, such as when you want to switch it on or off, or change the log level. The only thing you’ll get by doing it all in django is the possibility of ultra-fine control, such as only logging views of blog posts with id numbers greater than X or something. But generally you’d not want that level of detail, and you’d log everything and do any stripping at the analysis phase. You’ve not given any reason currently why you need to do it from Django.

If you really want it in a RDBMS, reading an apache log file into Postgres or MySQL or one of those expensive ones is fairly trivial.


One thing you should keep in mind is that SQL databases don’t offer you a very good writing performance (in comparison with reading), so if you are experiencing heavy loads you should probably look for a better in-memory solution (eg. some key-value-store like redis).

But keep in mind, that, especially if you would use a non-sql solution you should be aware what you want to do with the collected data (just display something like a ‘log’ or do some more in-deep searching/querying on the data).

If you want to identify different users from the same IP address you should probably look for a cookie-based solution (if you are using django’s session framework the session’s are per default identified through a cookie – so you could just simply use sessions). Another solution could be doing the logging ‘asynchronously’ via javascript after the page has loaded in the browser (which could give you more possibilities in identifying the user and avoid additional load when generating the page).

Leave a comment