not-a-monk has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tee and log files
by salva (Canon) on May 18, 2012 at 14:08 UTC | |
by not-a-monk (Novice) on May 18, 2012 at 15:21 UTC | |
by Anonymous Monk on May 18, 2012 at 19:55 UTC |