Reputation: 38714
I have a Perl web app and would like to log all uncaught exceptions (uneval'ed die's). My first thought is to do something like this:
use Carp 'cluck';
sub main {
my $logfile ="/some/path/logfile.txt";
open STDERR, ">>$logfile";
# main logic
...
}
sub eval_main {
eval {
main;
};
if ($@) {
cluck $@;
close STDERR;
# redirect to "friendly error page"
....
}
}
eval_main;
Is there a better way than this?
EDIT: Added redirect
Upvotes: 1
Views: 800
Reputation: 3219
Just use a $SIG{__DIE__}
handler. See %SIG in perlvar:
$SIG{__DIE__} = sub {
open LOG, ">>my/error.log";
print LOG @_;
close LOG;
print STDERR @_;
exit 1;
};
sub main {
...
}
main();
Upvotes: 5