I've searched the Internet and have found some good solutions for teeing STDOUT to 2 different places. Like to a log file and also to the screen at the same time. Here's one example:
use IO::Tee;
my $log_filename = "log.txt";
my $log_filehandle;
open( $log_filehandle, '>>', $log_filename )
or die("Can't open $log_filename for append: $!");
my $tee = IO::Tee->new( $log_filehandle, \*STDOUT );
select $tee;
But this solution leaves STDERR going only to the screen and I want STDERR go to both the screen and also to the same log file that STDOUT is being logged to. Is that even possible?
My task is to get my build process logged, but I also want to see it on my IDE's screen as usual. And logging the error messages is just as important as logging the happy messages. And getting the errors logged to a separate log file is not a good solution.
Simply reassign the
STDERR
filehandle ...Should mention that I tested this on Windows, it works, however I use StrawberryPerl.
So you want
STDERR
to behave likeSTDOUT
, going to both the screen and the same log file? Can you just dupSTDERR
with(I don't know offhand whether you would do this before or after the call to
IO::tee->new
).Though I use Log::Dispatch extensively, I've used the above to log what actually got displayed to the screen to a file.
I wrote a minimalistic perl logger with configurable dynamic logging giving you the following API:
You can redirect
stderr
tostdout
at the windows shell level by doing something like:See support article here for the official word.
Then you could use this stackoverflow answer to do a
tee
from the shell.I don't have a windows box to test this on, but perhaps you could do something like making a tied handle which will print to both STDOUT and a log, then redirecting STDOUT and STDERR to it?
EDIT: The only fear I have is the method of storing STDOUT for later use, I have added a second possibility for storing STDOUT for later use should the first not work on Windows. They both work for me on Linux.