Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Logging Errors (Instead of Just Dying)

Reply
Thread Tools

Logging Errors (Instead of Just Dying)

 
 
Hal Vaughan
Guest
Posts: n/a
 
      11-26-2005
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
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      11-26-2005
Hal Vaughan wrote:
> 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.

<snip>

> 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-26-2005
Hal Vaughan <(E-Mail Removed)> wrote:

> 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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      11-27-2005
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

Hal Vaughan wrote:

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logging to a file and closing it again properly (logging module) Christoph Haas Python 0 06-12-2006 09:58 PM
logging buffered vs. logging history Christian Roos Cisco 4 02-05-2006 10:55 PM
java.util.logging, where to put logging.properties? janne Java 0 09-10-2004 10:18 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
[java.util.logging] logging only to _one_ file Stefan Siegl Java 0 08-27-2003 12:29 PM



Advertisments