Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Building a log file

by Anonymous Monk
on Jun 23, 2009 at 14:47 UTC ( [id://774043]=perlquestion: print w/replies, xml ) Need Help??

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 $yourscript.pl | 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 tee.pl 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 #### tee.pl ###### # 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 ...

    cheers

    si_lence

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found