Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Problem logging STDOUT in perl

by kuldeek (Initiate)
on Aug 22, 2012 at 05:59 UTC ( #988954=perlquestion: print w/replies, xml ) Need Help??
kuldeek has asked for the wisdom of the Perl Monks concerning the following question:

I have a program, which call a subroutine and logs the STDOUT to a different file for each subroutine called.

foreach $test_run (keys(%test_hash)) { if ($test_hash{$test_run} == 1) { $test_execute = $test_run; $test_execute1 = $test_run."."."pm"; my $logfile = $test_execute."_log.txt"; print "the logfile is $logfile \n"; open (STDOUT, "| tee $logfile"); require "$test_execute1"; my $op = $test_execute->$test_execute(); }

On executing my script with above code creates all the log files, but lets say foreach loop runs for 3 times; then first log file have output of all the 3 calls of test_execute,2nd log file have output of 2nd and 3rd calls of test_execute and last log file will have only last call of test_execute. Please help me get all the logg files which contains logs for every single test_execute call.

Replies are listed 'Best First'.
Re: Problem logging STDOUT in perl
by aitap (Deacon) on Aug 22, 2012 at 07:21 UTC
    Perhaps you need to close STDOUT before opening? You can also use Capture::Tiny and/or IO::Tee instead of running tee.
    Sorry if my advice was wrong.
Re: Problem logging STDOUT in perl
by Athanasius (Chancellor) on Aug 22, 2012 at 07:39 UTC

    Hello kuldeek, and welcome to the Monastery!

    I’m not sure exactly what you want, but perhaps the select function is what you’re looking for:

    #! perl use strict; use warnings; use autodie; my %test_hash = ( Foo => 1, Bar => 1, Baz => 1, Nix => 0 ); foreach my $test_run (keys %test_hash) { if ($test_hash{$test_run} == 1) { my $test_execute = $test_run; my $test_execute1 = $test_run . '.pm'; my $logfile = $test_execute . '_log.txt'; print "the logfile is $logfile\n"; open(my $out, '>', $logfile); select $out; require $test_execute1; $test_execute->$test_execute(); close($out); select STDOUT; } }

    With suitable modules,, and, this code creates a log file for each module and redirects STDOUT to it from that module’s test sub. Output from print "the logfile is $logfile\n" is not redirected.

    See how do i redirect STDOUT, STDIN, or STDERR to a FILE?.

    Hope that helps,

    Athanasius <°(((><contra mundum

      Thanks for the reply. This will redirect logs only to log file, but I need logs to be redirected to log file as well on console i.e. STDOUT.

        Then you should definitely use a CPAN module, such as one of those recommended by aitap above:

        #! perl use strict; use warnings; use autodie; use Capture::Tiny qw( tee_stdout ); my %test_hash = ( Foo => 1, Bar => 1, Baz => 1, Nix => 0 ); foreach (keys %test_hash) { if ($test_hash{$_} == 1) { my $logfile = $_ . '_log.txt'; print "the logfile is $logfile\n"; require "$"; my ($stdout, @result) = tee_stdout { $_->$_() }; open(my $out, '>', $logfile); print $out $stdout; close($out); } }

        ++aitap for recommending Capture::Tiny! I already had it on my system (it came with Strawberry Perl), but I hadn’t come across it before. Brilliant!

        Athanasius <°(((><contra mundum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988954]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-12-15 05:37 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (417 votes). Check out past polls.