I have read several questions on how to use print function to write on file by redirecting standard output. I find it potentially useful for logging (where logging module is sometimes inconvenient), but I wonder if there is a way to send the output to multiple destinations. Basically suggested solutions are either:
- Reassign stdout with
sys.stdout=f
#f is something that looks like a file - Use
contextlib.redirect_stdout
So every time there is a print(something)
in code something
is redirected on f
. I am looking for something like
redirect_stdout([sys.stdout,f])
that outputs every something
to both file f
and the usual output.
Also, what is the timing of the print output (meaning if there is a function with multiple print statements, when is the content flushed on f and stdout, and what if there is an error?)?
I did some investigation, but couldn't find a solution, so I think this is above my knowledge.