I’m capturing the stderr of my script to I can later save to a database via:
ostderr = sys.stderr
sys.stderr = StringIO()
# do stuff
mymodel.errors = sys.stderr.getvalue()
mymodel.save()
print mymodel.errors
sys.stderr = ostderr
Unfortunately, this makes casual debugging more difficult because if any errors occur, I won’t see them until the script terminates.
How do I capture stderr in a string, as I’m doing above, but still display it in realtime to the console?
Use a file-like object that both saves and prints out to regular stderr. Something like:
You might need to support things like
closed,flush, etc as well, depending on what happens in#do stuff. This might be easier if you inherited fromStringIO.