elTriberium has asked for the wisdom of the Perl Monks concerning the following question:

Hi, everyone. I've got a problem that I've been thinking about for a while:

We have a custom Test Harness and we use the TAP::Parser module to start tests and parse their output. All test output currently just goes to STDOUT. However, I would also like this output to go into a logfile automatically. What would you consider the best way of doing that? Should I just add a "2>&1 | tee $logfile" to the 'exec' call of TAP::Parser? Or would you recommend using a real logging module (like Log::Dispatch)? I don't want to modify each test individually (there are too many!), the module that uses TAP::Parser should create the logfile automatically.

Thanks for any suggestions!

  • Comment on Logging output of a script to screen AND file

Replies are listed 'Best First'.
Re: Logging output of a script to screen AND file
by bobf (Monsignor) on Aug 07, 2010 at 03:34 UTC

    I have used IO::Tee in the past. IIRC, it was pretty simple and did the job.

    use IO::Tee; $tee = IO::Tee->new( $handle1, $handle2 ); print $tee "foo";

Re: Logging output of a script to screen AND file
by Khen1950fx (Canon) on Aug 07, 2010 at 03:07 UTC
    Here's an easy way to do it:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; open(LOG, '>', 'LOG_FILE') or die "Can't redirect stdout: $!"; open(CMD, 'ls |'); open(STDERR, '>&', STDOUT) or die "Can't redirect stderr: $!"; open(STDERR, '>', 'LOG_FILE') or die "Can't redirect stderr: $!"; print "LOG_FILE\n"; while (<CMD>) { print_stuff ($_); } sub print_stuff { my ($line) = @_; print LOG $line; print $line; } close(CMD) or die "close CMD failed: $!"; exit 0;