5

I have become aware that the print statements in my django app are causing wsgi to error and fail. Without going over all my code and removing/commenting out the print statements, is there a way I can disable them when running wsgi. It needs to be a simple as possible.

Thanks

Designer023
  • 193
  • 1
  • 1
  • 8

2 Answers2

9

Use WSGIRestrictStdout option:

WSGIRestrictStdout Off

or replace sys.stdout with sys.stderr in Django WSGI star script:

import sys
sys.stdout = sys.stderr

Writing To Standard Output

No WSGI application component which claims to be portable should write to standard output. That is, an application should not use the Python print statement without directing output to some alternate stream. An application should also not write directly to sys.stdout.

ooshro
  • 11,502
2

In general, it is better to use logging for any kind of debug output in a web app and never use print statements at all. Or use them in the form:

print >> environ['wsgi.errors'], "message to be printed"

It is best to get rid of sys.stdout by making it a copy of sys.stderr just in case.

If you do development on a UNIX server, you can have two terminal windows open, one to run Django and the second one to do

tail -f loggingfile

And you get pretty much the same effect as using print statements, but you can use the standard logging module to generate the output, and in the production app, you just change the filter setting to not show DEBUG level messages.