Logging Errors (Instead of Just Dying)

H

Hal Vaughan

I have several Perl daemons running on a particular system. For the most
part, they are behaving well, but I notice that two need to be restarted a
few times a week. I have a lot of logging statements saying what the
program is doing at different points. What I'd like to do is log the
actual errors before the program dies (and is then restarted by it's
monitor). In the routine, I want to be able to print the line number and
type of error (as given in the normal error messages) to my log file.

Is there any way, inside a Perl module, to do something like "If there are
any errors, don't die, call this function, THEN die"? (Or just "call this
function" and I can always make the function itself terminate the program.)

I know the FAQ mentions using eval, but they reference Larry Wall's
"&realcode", which, when I Google it, comes up with references to the same
FAQ, where it is not explained. I'm trying to understand using eval, but I
don't see how I could put an entire module in an eval block -- I would
expect that to only evaluate it when the module is loaded. The few
examples of this (that I can find) are, at least to me, hard to follow, so
clearer examples would help a LOT. (I'm wondering if, since it's in the
FAQ, nobody's bothered to cover this in depth or with more examples.)

For example, if most of what the program does is in the module, how would I
use eval around the subroutine calls from the main program? Can I get away
with having one call to the mainloop (which is in a subroutine by itself)
in it and then using eval only around that one call to the main loop?

Any help in clarifying this is appreciated!

Hal
 
M

Mark Clements

Hal said:
I have several Perl daemons running on a particular system. For the most
part, they are behaving well, but I notice that two need to be restarted a
few times a week. I have a lot of logging statements saying what the
program is doing at different points. What I'd like to do is log the
actual errors before the program dies (and is then restarted by it's
monitor). In the routine, I want to be able to print the line number and
type of error (as given in the normal error messages) to my log file.

Is there any way, inside a Perl module, to do something like "If there are
any errors, don't die, call this function, THEN die"? (Or just "call this
function" and I can always make the function itself terminate the program.)

You could define an END block and put some logging in there (see perldoc
perlmod), and/or reinvoke your program via exec. You may have difficulty
accessing error messages etc at this stage though.

For example, if most of what the program does is in the module, how would I
use eval around the subroutine calls from the main program? Can I get away
with having one call to the mainloop (which is in a subroutine by itself)
in it and then using eval only around that one call to the main loop?

not entirely sure what you mean, but you could try:

eval {
my $serviceLoop = ServiceLoop->new();
$serviceLoop->run();
};

if($@){
print "error in serviceloop => $@";
}else{
print "exited serviceloop cleanly";
}

I also suggest checking out Log::Log4perl (or similar). Note this can be
set up to display the location at which a message is output.

Mark
 
T

Tad McClellan

Hal Vaughan said:
What I'd like to do is log the
actual errors before the program dies (and is then restarted by it's
monitor). In the routine, I want to be able to print the line number and
type of error (as given in the normal error messages) to my log file.

Is there any way, inside a Perl module, to do something like "If there are
any errors, don't die, call this function, THEN die"? (Or just "call this
function" and I can always make the function itself terminate the program.)

For example, if most of what the program does is in the module, how would I
use eval around the subroutine calls from the main program?

eval { subroutine_call( $arg1, $arg2) };
exit_gracefully($@) if $@; # call exit_gracefully() if error was trapped
 
H

Hal Vaughan

Just a quick 'thanks!' to those who replied. When I was reading the FAQ, it
looked more complex, and the examples here showed me just how easy this is!
I wish I had known about this a long time ago. I've already set it up,
roughly like this:

eval {
mainloop();
}
if ($@) {
serverlog("error Perl has reported an error: $@");
serverlog("exiting so program will restart");
exit();
}

There was just enough in the faq (and enough left out) to make it seem like
it was more complex than it is.

Your help is appreciated!

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top