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

Hello all, Just wondering if there is any way to build a log file of all the print statements within my script. Is there an easy way, or do I have to assign varible and appaend each print statement to the string, then write it to the log file?

Replies are listed 'Best First'.
Re: Building a log file
by davorg (Chancellor) on Jun 23, 2009 at 14:50 UTC
    • If you're starting a new project then you should look at something like Log::Log4perl.
    • A quick 'n' dirty solution is to just run your program redirecting output to a file (i.e ./my_prog > some_file.log).

    See the Copyright notice on my home node.

    Perl training courses

Re: Building a log file
by moritz (Cardinal) on Jun 23, 2009 at 14:52 UTC
    Sure, perl $ | tee logfile.

    You can also duplicate the STDOUT file handle as described in perlopentut or open.

      This is a good idea. Windows systems don't have tee, I hacked in this awhile ago for my Windoze work. It is a simple program. The only tricky part is turning auto-flush on for write. Have fun to anybody who needs it.
      #!/usr/bin/perl -w use strict; # tee program #### ###### # quickie tool $| =1; #flushes I/O for each write. sub usage { print "TEE USAGE:\n tees stdout to a file and to stdout\n". " program | tee outfile\n"; } my $filename = shift @ARGV; usage unless $filename; open (OUTFILE, '>', "$filename") or (die "Can't open OUTFILE: $!"); while (<>) { print; print OUTFILE; }
Re: Building a log file
by si_lence (Deacon) on Jun 23, 2009 at 14:56 UTC
    I'm not sure I understand your problem.

    You can open your log-file at the start of your program and just write to it with the print statements:

    my $logfile = 'path/to/logfile.txt'; open(LOG, "> $logfile") or die "could not open log-file: $!"; print LOG "this is a log entry\n";

    If you need to see your log output immediately be aware of buffering issues ...