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 ("./1_setup_dirs.pl -a $object")
if ($@) {
write_log (%log = (
event_level => 'error',
role_id => "$global{$dmn}{'role_id'}",
func_id => $func_id,
message => "$@",
)
);
Thanks in advance.