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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.