[Tutor] redirect stdout and stderr?

Brian Wisti bwisti@hotmail.com
Thu, 20 Jul 2000 11:10:42 PDT

Hi all,

I am at a bit of a loss here.  What I'm trying seems simple enough: make my 
application send stdout (debug) messages to one file, and stderr (error) 
messages to a different file.

The Python Essential Reference tells me that sys.stdout and sys.stderr hold 
file objects, and all you need to do for redirection is make them hold 
different file objects.

Thinking myself clever, I applied the following code:

(I'm not sure how Hotmail will treat this, so I'll wrap it in pre tags)

class MyHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, RequestHandlerClass):
                BaseHTTPServer.HTTPServer.__init__(self, server_address, 

		# Open files for output redirection
                sys.stdout = open('message.log', 'a')
                sys.stderr = open('error.log', 'a')
                print "Starting server"

        def __del__(self):
                print "Shutting down server"
		# Close redirect files

The theory is that the HTTP server, which is created as soon as the program 
starts, will send all of its messages to these two files.  It will also be 
kind enough to close those files when it shuts down.

I am obviously missing something, though.  The files are created (if they 
don't already exist), but nothing is ever written to them.  Sadly, nothing 
goes to the terminal, either.

Do I need to close and reopen these files any time the program has something 
interesting to say?  That would be annoying, but not impossible.

Please forgive my ignorance.  I know very little about file handling, and 
even less about file handling in Python.

Any guidelines or strategies would be very helpful!

Thanks in advance,
Brian Wisti

Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com