This code does not work as I would like. The result on the terminal is correct except for STDERR (but I can live with that if needed). However, the log file displays all of the data I write to the log file before all the data I write to STDOUT, and I want to preserve the order so I can do 'tail -f' to see log file additions in real-time.#!/usr/bin/perl open(STDERR,">&STDOUT"); # Redirect IO select(STDOUT); $| = 1; # Unbuffered output # Set the path and format of the name for z_brian's log file $logfileName = sprintf("$ENV{LTMP}/z_brian_%02d\_%02d\_%02d.log", $yea +r, $mon, $mday); # Write everything that goes to standard out also to the log file open(STDOUT, "| tee -ai $logfileName"); # Open a file handle to that same log file to also write verbose info +to open(my $LOGFILE, ">>", $logfileName) or die $!; print "Welcome user!\n"; print $LOGFILE "log msg 1\n"; print STDOUT "out msg 1\n"; print $LOGFILE "log msg 2\n"; print STDERR "err msg 1\n"; print $LOGFILE "log msg 3\n"; print "out msg 2\n"; print $LOGFILE "log msg 4\n"; print "Goodbye user!\n"; print $LOGFILE "log msg 5\n";
In reply to tee and log files by not-a-monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |