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

Hi monks,

As title, I'd like to save error to a file because I check script running once a week.I supposed that changing STDERR to a file handle is a valid way. But I'm not sure.Please help.

UPDATE:
Thank Zaxo! your reply inspired me to find open documentation. and I have found answer in it. But I'm still curious if TMTOWTD?


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
  • Comment on How can I save error message to a file?

Replies are listed 'Best First'.
Re: How can I save error message to a file?
by Zaxo (Archbishop) on Jun 12, 2007 at 01:40 UTC

    If it's a special run, you can just redirect (./foo 2> /path/to/testlog.txt) from the command line. Otherwise,

    open local(*STDERR), '>>', '/path/to/testlog.txt' or die $! if $TESTING;
    You can remove the "if $TESTING" clause if you want to log all errors from this code. If you do that, you may want to include a timestamp in your warnings and dying words.

    After Compline,
    Zaxo

Re: How can I save error message to a file?
by andreas1234567 (Vicar) on Jun 12, 2007 at 06:50 UTC
    I highly recommend Log::Log4perl. Combined with Log::Dispatch::FileRotate you have a very powerful logging mechanism that will write your error messages to file.
    --
    print map{chr}unpack(q{A3}x24,q{074117115116032097110111116104101114032080101114108032104097099107101114})
Re: How can I save error message to a file?
by neverminda (Novice) on Jun 12, 2007 at 01:32 UTC
    I actually changed the STDERR to a file handle, but somehow it didn't work and the errors didn't end up in the file.

    One way to save the errors into a file is to save all your output(including errors) into a file. You can run your "perlprogram.pl" as "perlprogram.pl >>log.dat".

    This is for unix environment. I'm not sure if it works for PCs.

Re: How can I save error message to a file?
by sago (Scribe) on Jun 12, 2007 at 05:55 UTC
    In windows, open(OUT,">>d:/project/prod/logs/test.log")|| print "could not open logfile\n"; . . . close OUT;