Configure the output of the Django development server

django-logI recently opened up my home dev server for a closed, friends and family alpha launch of my new commerce/community site Wantbox.com. Now that people besides me are hitting the server, I wanted to see client IP addresses in the dev server output.

Current output:

[19/Sep/2010 19:23:39] "GET / HTTP/1.1" 200 2555
[19/Sep/2010 19:23:39] "GET /static/media/css/site.css HTTP/1.1" 200 14311
[19/Sep/2010 19:23:40] "GET /static/media/js/site.js HTTP/1.1" 200 620
[19/Sep/2010 19:23:40] "GET /static/media/img/logo-home.gif HTTP/1.1" 200 4963

Desired output:

[19/Sep/2010 19:23:39] 166.137.139.17 "GET / HTTP/1.1" 200 2555
[19/Sep/2010 19:23:39] 166.137.139.17 "GET /static/media/css/site.css HTTP/1.1" 200 14311
[19/Sep/2010 19:23:40] 166.137.139.17 "GET /static/media/js/site.js HTTP/1.1" 200 620
[19/Sep/2010 19:23:40] 166.137.139.17 "GET /static/media/img/logo-home.gif HTTP/1.1" 200 4963

I wasn’t able to track down any built-in configuration options to runserver, but thanks to the sage advice of “Python/Django evangelist” Peter (most recently, the creator of The Prince of Pinot) I got this workaround (applies to Django 1.2.1):

  1. open django/core/servers/basehttp.py
  2. change line 614
    • from:  msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)
    • to:  msg = "[%s] %s %s\n" % (self.log_date_time_string(), self.client_address[0], format % args

I’m not crazy about messing with the core Django code, but I found no other way to get the output I wanted short of running a “real” webserver like Apache, which I didn’t want to do in my closed, low traffic dev environment. By all means, if you have a better solution, let me know about it in the comments.

Also, if you happen to live in the greater Boston metro area and would like to help a local Django developer improve a new site, ping me on twitter (@mfournier). I’d love your feedback.