Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
reduce log level for some errors when run in crontab context
  • Loading branch information
sni committed Nov 6, 2024
1 parent fd82cbe commit 36e05fb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/Thruk/Backend/Provider/Mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ sub _update_logcache {
_debug($error);
$error = $short_err;
}
_error('logcache '.$mode.' failed: '.$error) unless $error =~ m/(.*\Qplease come back later\E|\QConnection refused\E)/mx;
_cronerror('logcache '.$mode.' failed: '.$error); # don't fill the log with errors from cronjobs
die($error);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Thruk/Utils/CLI/Logcache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ sub cmd {
$err = $short_err;
}
$err = sprintf("backend '%s' failed: %s", $backend, $err);
_error($err);
_cronerror($err); # don't fill the log with errors from cronjobs
}
my $elapsed = tv_interval($t1);
return($log_count, $plugin_ref_count, $err, $elapsed);
Expand Down
6 changes: 1 addition & 5 deletions lib/Thruk/Utils/Cluster.pm
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,7 @@ sub run_cluster {
_debug($msg);
} else {
if(!$node->{'last_error'} && !$node->{'maintenance'}) {
if($ENV{'THRUK_CRON'}) {
_warn($msg);
} else {
_error($msg);
}
_cronerror($msg); # don't fill the log with errors from cronjobs
} else {
_debug($msg);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/Thruk/Utils/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use Thruk::Base ();
use Thruk::Utils::Encode ();

use base 'Exporter';
our @EXPORT_OK = qw(_fatal _error _warn _info _infos _infoc
our @EXPORT_OK = qw(_fatal _error _cronerror _warn _info _infos _infoc
_debug _debug2 _debugs _debugc _trace _audit_log
_debug_http_response
);
Expand Down Expand Up @@ -68,6 +68,13 @@ sub _error {
return &_log(ERROR, \@_);
}

##############################################
# like _error, but only emits a warning if run from cron
sub _cronerror {
return &_log(WARNING, \@_) if $ENV{'THRUK_CRON'};
return &_log(ERROR, \@_);
}

##############################################
sub _warn {
return &_log(WARNING, \@_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ sub _action_facts {
}
if(!$facts || $facts->{'last_error'}) {
my $err = sprintf("%s updating %s failed: %s\n", $peer->{'name'}, $mode, ($facts->{'last_error'}//'unknown error'));
if($ENV{'THRUK_CRON'}) {
_warn($err); # don't fill the log with errors from cronjobs
} else {
_error($err);
}
_cronerror($err); # don't fill the log with errors from cronjobs
} else {
_info("%s updated %s sucessfully: OK\n", $peer->{'name'}, $mode);
}
Expand Down

0 comments on commit 36e05fb

Please sign in to comment.