Quote:
|
Originally Posted by Sane
I need to redirect all console output (including error messages) to a log file. There are several reasons why I would have to go out of my way to change each print statement to a call to a function. It would be much easier if there's some support in the sys library for just that?
Any helpful info?
|
It's not a great way of doing things, but you can just set sys.stdout to your logfile:
import sys
oldstdout = sys.stdout # So you can restore later
sys.stdout = file("C:/temp/test.txt", "w") # Or whatever your logfile is
for i in range(10):
print "Hello", i
sys.stdout.close()
sys.stdout = oldstdout An alternative might be to do a find and replace of all instances of "print " to "print>>STDERR,". Then at the top of your module or config file define STDERR as your logfile. If you want to have things print to screen then change STDERR to sys.stderr.
Another alternative would be to import logging and actually log things properly rather than using prints.
-T.