use strict; use warnings; use open qw(:std :utf8); # Set the default encoding for STDIN, STDOUT & STDERR to UTF-8 use IO::Tee (); my $logname = 'test-tee$.log'; open(my $logfile, ">:encoding(UTF-8)", $logname) or die qq(Can't open "$logname for writing"\n); # Tee STDERR to log file. If running as Administrator, console window may disappear quickly! open STDERR2, ">&=STDERR" or die "Failed to alias STDERR: $!"; *STDERR = IO::Tee->new(\*STDERR2, ">$logfile") or die "Failed to tee to aliased STDERR and '$logfile': $!"; print(STDERR "This is a test\n");