Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Centralised error handling

by kazak (Beadle)
on Oct 26, 2012 at 07:48 UTC ( #1001019=perlquestion: print w/replies, xml ) Need Help??
kazak has asked for the wisdom of the Perl Monks concerning the following question:

Hi again to all community. I made a set of scripts and these scripts on different servers, now I'm trying to make them to write a central Mysql-based error log and there are two types of situations with it. First situation is when "warning\error" defined by my logics (like lack of some definitions in config or attempt to create an object that already exists, etc) - my code works as a charm. And the second situation when error\warning caused by faulty DB connection, wrong mysql query, or returned by child script executed through "system()" call in my main script. So my question is: Is this possible to unify an interception of these kinds of errors and which approach is better for that? How I'm trying to do it, function:
sub write_log { my $my_func_id = "write_log"; print "$my_func_id EVENT_LEVEL: $log{'event_level'}\n" if ($de +bug eq "1"); print "$my_func_id LABEL_ID: $label_id\n" if ($debug eq "1"); print "$my_func_id ROLE_ID: $log{'role_id'}\n" if ($debug eq " +1"); print "$my_func_id FUNC_ID: $log{'func_id'}\n" if ($debug eq " +1"); print "$my_func_id MESSAGE: $log{'message'}\n" if ($debug eq " +1"); my $do_write_log = "INSERT INTO `$log_tbl` (`event_id`, `event +_level`, `label_id`, `role_id`, `func_id`, `message`, `event_date`) +VALUES (NULL, '$log{'event_level'}', '$label_id', '$log{'role_id'}', +'$log{'func_id'}', '$log{'message'}', CURRENT_TIMESTAMP);"; print "$my_func_id QUERY: $do_write_log\n" if ($debug eq "1"); my $sth0 = $dbh->prepare($do_write_log); $sth0->execute(); $sth0->finish(); }
And how I'm calling it in case of "warning\error" event and it works:
write_log (%log = ( event_level => 'warning', role_id => "$global{$ln}{'role_id'}", func_id => $func_id, message => "NOTIFY(site or db_user) - @site_entry EXISTS,ABORTING" +, ) );
But may be I'm totally wrong when I'm trying to use my code like this:
my $rv = $sth->execute(); if (!$rv) { write_log (%log = ( event_level => 'warning', role_id => "$global{$dmn}{'role_id'}", func_id => $func_id, message => "$dbh->errstr", ) ); Or like: system ("./ -a $object") if ($@) { write_log (%log = ( event_level => 'error', role_id => "$global{$dmn}{'role_id'}", func_id => $func_id, message => "$@", ) );
Thanks in advance.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001019]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-05-22 22:02 GMT
Find Nodes?
    Voting Booth?