Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

printing to STDOUT and a Logfile

by oroburos (Initiate)
on Oct 29, 2002 at 00:34 UTC ( #208643=perlquestion: print w/ replies, xml ) Need Help??
oroburos has asked for the wisdom of the Perl Monks concerning the following question:

Monks: How can I print to STDOUT and a logfile at the same time? I am trying to capture the results of an installation script. Of course I need to display the contents to STDOUT but I really do not want to create two print statements if it is not necessary, Thanks for any assistance Mike

Comment on printing to STDOUT and a Logfile
Re: printing to STDOUT and a Logfile
by rbc (Curate) on Oct 29, 2002 at 00:39 UTC
    This is the way I do it. It my be too simple for all your logging needs.
    perl -e 'print "Hello\n";' | tee logfile
    ... or If I also want to trap STDERR I do ...
    perl -e 'print "Hello\n;' >& logfile
    ... but then again does logging need to be complicated?
Re: printing to STDOUT and a Logfile
by Ovid (Cardinal) on Oct 29, 2002 at 00:42 UTC

    If you're on a Unix-like system, you can check out the 'tee' command:

    
    $ man tee
    
    NAME
           tee  - read from standard input and write to standard out-
           put and files
    

    Another option, which I have never used, is Tie::FileHandle::MultiPlex.

    Update: One way that I have used 'tee' in the past.

    use constant TEE => '| tee '; use constant TO_PRINTER => "| lp -d lex_text"; my $file = REPORT_DIR . "cc_auth_$DATETIME"; open CCAUTH_REPORT, TEE . $file . TO_PRINTER or die $!;

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Another option, which I have never used, is Tie::FileHandle::MultiPlex.

      ... and if you hunt around CPAN, you'll also find Filter::tee and IO::Tee, which look to be of a similar vein.

      Update: D'oh... shoulda tested the code in the <readmore> before posting. Although it prolly still could be made to work, ybiC's code is simpler.

          --k.


Re: printing to STDOUT and a Logfile
by jsprat (Curate) on Oct 29, 2002 at 01:15 UTC
    You could always use IO::Tee. For example,

    #!/usr/bin/perl use strict; use warnings; use IO::Tee; open my $log, '>stdout.log' or warn "Can't write logfile: $!"; my $tee = new IO::Tee(\*STDOUT, $log); # Use it like a file handle print $tee "Just another perl hacker,\n"; # Use the print method of the IO::Tee object $tee->print("Just another perl monk.\n"); $tee->flush; __END__ Output: C:\S\Test>iotee.pl Just another perl hacker, Just another perl monk. C:\S\Test>type stdout.log Just another perl hacker, Just another perl monk.

    Hope it's helpful,

    Update: simplified the example

Re: printing to STDOUT and a Logfile (cross-platform sub)
by ybiC (Prior) on Oct 29, 2002 at 01:22 UTC

    Here's one cross-platform way to simultaneously print to STDOUT and a filehandle.

    # print messages to both console and logfile sub PrintLogCon { print @_; print(LOG @_) or die "Error printing to $logfile: $!"; }

    and you'd call it thusly:

    use warnings; use strict; open(LOG, ">$logfile") or die "Error opening $logfile: $!"; ... ... ... do->stuff; do->it(again) or PrintLogCon("Error: $!"); do->stuff(more); ... ... ... close LOG or die "Error closing $logfile: $!"; __END__
    Phat props to mad monk Petruchio for showing me this a while back.
        cheers,
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")

    Update2: consistant-ized scalar for logfile
    Update1: or use IO::Tee as shown by fine monk jsprat.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://208643]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2015-07-06 00:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls