Search code examples
perllogginghttp-redirectstdoutstderr

Can I send STDOUT and STDERR to a log file and also to the screen in Win32 Perl?


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.


Solution

  • You can redirect stderr to stdout at the windows shell level by doing something like:

    perl stuff.pl 2>&1
    

    See support article here for the official word.

    Then you could use this stackoverflow answer to do a tee from the shell.

    perl stuff.pl 2>&1 | tee stuff.txt