Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling question 1

Status
Not open for further replies.

rpack78

Programmer
Oct 1, 2004
35
US
How do I log perl errors to a file of my choice when my perl script dies. For example, I am passing a date to the DateCalc method in Date::Manip. If I pass an invalid date, the script dies. If I run it on the command line, I see the error:

ERROR: Invalid year (928)
at C:/Perl/site/lib/Date/Manip.pm line 5507
Date::Manip::Date_FixYear(928) called at C:/Perl/site/lib/Date/Manip.pm
line 3350...

But I want to view this error later on using a web browser and need to know how to log it. Also I am not running this manually on the command line, it is running from cron. Any help would be appreciated!

Ryan
 
You could redirect error output to a file:
[tt]
perlscript.pl 2>logfile
[/tt]
 
if you just need to see the error, add this line to your CGI script:

use Carp::CGI 'fatalsToBrowser';

fatal error will be printed to the browser.
 
Sorry, I missed the cron part somehow so my reply is really not relevant. Tony hooked you up though.
 
Thanks Tony, that is what I was looking for. But now I have a new problem. I was doing this:

perlscript.pl >logfile

because I am logging everything that is printed in the script. If I do this:

perlscript.pl 2>logfile

I no longer log that stuff but I do trap the errors. Is there a way to log both?
 
To produce separate logs, you could do:
[tt]
perlscript.pl >logfile 2>errorfile
[/tt]
or to log everything in the one file:
[tt]
perlscript.pl >logfile 2>&1
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top