I spent a considerable amount of time reading documentation that told me how to send errors to the Apache log filewhen you use mod_wsgi. I read documentation on how to integrate mod_wsgi with Apache and Django. All of this was fine, but when I was trying to get it to work with my Django 1.0.2 installation and Apache2 running mod_wsgi, I was constantly greeted by lots of errors. One in particular was “sys.stdout access restricted by mod_wsgi”. The documents online didn’t help at all.
Finally, after looking into the django.core.handlers.wsgi module, I figured out how to send errors to the wsgi.errors setting and subsequently send errors to your Apache2 Error.log file.
I started off with my views.py as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | from django.http import HttpResponse from django.shortcuts import render_to_response, get_object_or_404 from myapp.models import F def index(request): c = {} return render_to_response('myapp/index.html',c) def detail(request, name): n = F.objects.get(name=name) c = {'name': n.fof} return render_to_response('myapp/detail.html',c) |
If I needed to write to the Error.log file, I would have to change the code thusly:
1 2 3 4 5 6 7 8 9 10 11 12 13 | from django.http import HttpResponse from django.shortcuts import render_to_response, get_object_or_404 from myapp.models import F def index(request): c = {} print >> request.environ['wsgi.errors'],"Teh Errorist!" return render_to_response('myapp/index.html',c) def detail(request, name): n = F.objects.get(name=name) c = {'name': n.fof} return render_to_response('myapp/detail.html',c) |
The change is in line 7
I felt really stupid, because the solution was so straightforward. I felt even more stupid that I couldn’t find any documentation out there. Either way, I’m pleased that my app works, and in case someone else out there is searching for how to achieve this, then the above is how to do it.


Discussion
View Comments for “Sending messages to the Apache Error.log file when using Django”