Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Printing both to console and File

by perluser455 (Initiate)
on Apr 17, 2017 at 11:47 UTC ( #1188129=perlquestion: print w/replies, xml ) Need Help??
perluser455 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, i am writing a perl code to run one of my products and as part of that i wanted the log file to print both STDERR and STDOUT to log and Blackscreen. Below is code am using currently and it prints all the details to Blackscreen but only STDOUT to file , i would like to print even STDERR ,am using strawberry perl. Any suggestions here can be of great help. Code:
my $log_filehandle; open( $log_filehandle, '>>', $log_filename ) or die("Can't open $log_filename for append: $!"); my $tee= IO::Tee->new( $log_filehandle, \*STDOUT ); my $tee2= IO::Tee->new( $log_filehandle, \*STDERR ); ##*STDERR = *$tee{IO}; --Adding or removing gives same output select $tee; -------my complete program code below-----------

Replies are listed 'Best First'.
Re: Printing both to console and File
by 1nickt (Monsignor) on Apr 17, 2017 at 11:56 UTC

    The doc for IO::Tee indicates (unsurprisingly) that you need to pass two filehandles to the constructor, not construct two instances:

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

    For another approach see Log::Any, Log::Any::Adapter, Log::Any::Adapter::Dispatch.

    Hope this helps!


    The way forward always starts with a minimal test.
      Thanks for the reply, but Can you Please let me know how to pass stdout/stderr to filehandles when declared as use IO::Tee; $tee = IO::Tee->new($handle1, $handle2);

        It so happens that the example that contains the answer to your question is in the example section of IO::Tee's documentation.

        Edit: wait, you already use the \*STDOUT notation in your OP, I must be misunderstanding your question.

Re: Printing both to console and File
by soonix (Monsignor) on Apr 17, 2017 at 12:49 UTC
    The problem is that IO::Tee->new gives you a new handle, and your select call only changes the default file handle of print from STDOUT to the new handle.

    It seems to me, that wrapping your code into Capture::Tiny->tee (which internally uses IO::TeeHandle) would solve your problem.

Re: Printing both to console and File
by Anonymous Monk on Apr 17, 2017 at 16:37 UTC
    my $tee = Ice::Tea->new( $log_console, \*STDMIC );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-07-21 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (442 votes). Check out past polls.

    Notices?