6

I'm getting started with developing Django code on a server, running on top of Apache/mod_wsgi.

I'm looking to understand a few things:

  1. What techniques are normally used to debug applications running on the server?

  2. Specifically, I'm trying to just use "print" debugging for now. But I can't seem to get print statements to work. I'm printing to stderr, but I'm not sure which log file I should be looking at. According to this, I should be using environ['wsgi.errors'], but how do I access that from my Django code?

Thanks!

EDIT: By the way, adding the line print >> sys.stderr, 'message ...' not only doesn't seem to print to any log file, it causes parts of my application to simply not load.

5
  • 2
    I figured out how to get access to environ. Simply do this: request.META['wsgi.errors'].write("ello world"). Commented Jan 27, 2011 at 8:51
  • 1
    Using 'print' as you show it, being directed to stderr should work fine. Which Apache error log file it goes to depends on whether you are using embedded mode or daemon mode and whether you are using ErrorLog directive inside of the VirtualHost. If it isn't coming out in log, then what fiddles are you doing to sys.stdout/sys.stderr in your WSGI script file if any. Commented Jan 27, 2011 at 8:53
  • 2
    Use can also use "print >> request.META['wsgi.errors'], 'hello world'". Commented Jan 27, 2011 at 8:54
  • @Graham Dumpleton: I set up a custom log directory inside my VirtualHost, but errors aren't getting output, neither to the log dir nor to the /var/log/apache dir, which is the main log dir AFAIK. But that's not the weird part, the weird part is that my application doesn't seem to work when I use print >> sys.stderr. I would assume that even if my log file isn't working properly, the rest of the application should work fine. Commented Jan 27, 2011 at 8:59
  • Define don't work, but don't do it here. Suggest you go onto the mod_wsgi mailing list as this isn't the place to debug problems. Commented Jan 27, 2011 at 9:33

1 Answer 1

4
  1. Try using the django debug toolbar. It can help a great deal with debugging when you can't actually use a debugger.

    Really though, debugging should be done on your development machine. I have yet to see a code issue in production with django that wasn't also occurring on my dev box.

  2. You usually can't print in mod_wsgi. Use the logging module instead. That's really what you want, and the debug toolbar will show you log statements in the page, so you don't even have to look at the file.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm trying to use the logging module. I've added, at the top of my views.py, the first lines from "simple examples", but I'm getting a "permission denied" IOError (I'm trying to log to my home directory). Is there anything I need to do to give permissions?
I found the problem - the user Apache is running under is not the user I log in as, so the home directory is off-limits. I gave everyone permissions for the log directory using chmod a+w logs. Do you have any recommendations on reading for how to set up logging problem with Django? I have a feeling that just adding the code to the top of my view.py is not such a great idea.
On any mod_wsgi version you can use "print >> sys.stderr, 'message ...'". Use the latest mod_wsgi and you can quite happily use 'print' without directing it to stderr. It was blocked for mod_wsgi <3.0 to encourage you to write portable WSGI applications, but then no one seem to care about that. Read 'blog.dscpl.com.au/2009/04/…' for the history and reasons.
Yes I already saw that post, but as I explained above, neither normal "print" nor "print >> sys.stderr" seemed to work. Using request.META does work, as does logging, so I guess I'm sticking with that. Thanks for the help in any case!
Then your environment/configuration is broken in someone as they should work. Suggest you use a WSGI hello world script to test it and leave aside any framework or your own application code to ensure they are not the cause of the problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.