Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
[LanX]: choroba IIRC these errors are indicated
[1nickt]: Missing title suppresses the submit button, I think.
[1nickt]: Petroza to answer your question, no, no special permission is needed to post a question.
[LanX]: did you spam before? :)
LanX has to go/
[ambrus]: I hope we didn't mess up the spam filter rules again.
[ambrus]: Our spam filter rules disallow links to certain domains, and some suspicious pharses that have appeared in previous spam advertising cheap online whatevers.
[LanX]: some servers were lagging today, so I suppose the root cause
[LanX]: ambrus no recent patches
[Petroza]: no i haven't posted anything before. It was a more or less long question with a specific issue. I did post a title and the links i added where only part of the element i was searching within the code (so no purpose other than the question itself).

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2017-10-17 15:21 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (233 votes). Check out past polls.