Seeing what gets written to stderr in Python web site using FastCGI-Collection of common programming errors
I am working on a website, hosted on DreamHost, using Python. For a while, I was using their default setup, which runs Python scripts using CGI. It worked fine, but I was worried that if I get a lot of traffic, it would run slow and use a lot of memory, so I switched it over to FastCGI using this module.
Overall, it still works fine, but there is one major annoyance: I can’t seem to be able to see anything that gets written to the standard error stream. If anything goes wrong, my usual source of useful clues for what to do about it no longer works. Before, I used to see stuff sent to standard error in my Apache error log. Now, it just seems to disappear.
I tried making a test script, and writing strings using sys.stderr.write (from various places), and environ[“wsgi.errors”].write (from within my app, where environ is the first parameter passed to the app by the WSGI/FastCGI wrapper). Either way, I couldn’t find them. Does anyone know why, or how to access this data?
Keep in mind that this is my first time ever using FastCGI, so please let me know if I am making a bad choice by using this fcgi module.
-
If something in your system is capturing file-descriptor two (the “real” stderr), you can assign
sys.stderr
to any open, writeable file object, or to a file-like object (it basically just needs to implementwrite
) — including acStdIO.StdIO
instance, whose value you can get at any time (before it’s closed) with a call to its.getvalue()
method.To capture any uncaught exception just before it terminates your code, assign to sys.excepthook a function of yours in which you get the information and emit it in any way of your choice; or, to get and emit anything that was written to
sys.stderr
even without an exception (if that’s what you want — I’m not sure, from your question), use atexit to register your grab-info-and-emit-it function.