Make logging use features of Log::Any and log as it should be done via this module. Cleanup sample config.

This commit is contained in:
eleksir
2021-05-04 18:04:32 +03:00
parent 095cb57a18
commit 096877bee2
15 changed files with 104 additions and 78 deletions

View File

@@ -29,6 +29,7 @@ no Cwd;
use Cwd::utf8 qw (chdir abs_path);
use File::Basename qw (basename dirname);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Log::Any::Adapter;
use Log::Any::For::Std;
use POSIX qw (setgid setuid setsid);
@@ -44,6 +45,7 @@ my $runAsUid = $UID;
my $piddir = '/var/run/' . basename (abs_path ($PROGRAM_NAME));
my $C = LoadConf ();
my $loglevel = $C->{'loglevel'} // 'notice';
my $logfile;
if (defined $C->{'log'}) {
@@ -92,7 +94,7 @@ if ($UID == 0) {
}
if (defined $logfile) {
Log::Any::Adapter->set ('File', $logfile);
Log::Any::Adapter->set ('File', $logfile, log_level => $loglevel);
# Some annoying CHI logging events come with category set to CHI::Driver and log_level set to debug.
# Until we use start using Log::Any native features and set whole loglevel accordingly, lets lower
@@ -116,14 +118,24 @@ daemonize ();
my $apath = abs_path $PROGRAM_NAME;
unless (defined $apath) {
die "[FATAL] Unable to get abs_path: $OS_ERROR\n";
$log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR\n");
exit 1;
};
# write pidfile
my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath;
warn "[INFO] Creating pidfile at $pidfile\n";
open my $MYPID, '>', $pidfile or die "[FATAL] Error opening $pidfile: $OS_ERROR\n";
print {$MYPID} $PID or die "[FATAL] Error writing to $pidfile: $OS_ERROR\n";
$log->info ("[INFO] Creating pidfile at $pidfile");
open my $MYPID, '>', $pidfile or do {
$log->fatal ("[FATAL] Error opening $pidfile: $OS_ERROR");
exit 1;
};
print {$MYPID} $PID or do {
$log->fatal ("[FATAL] Error writing to $pidfile: $OS_ERROR");
exit 1;
};
close $MYPID; ## no critic (InputOutput::RequireCheckedSyscalls
local $SIG{INT} = \&__signal_handler;
@@ -157,7 +169,8 @@ sub __fork {
my $pid = fork;
unless (defined $pid) {
die "[FATAL] Can't fork: $OS_ERROR\n";
$log->fatal ("[FATAL] Can't fork: $OS_ERROR");
exit 1;
}
if ($pid != 0) {
@@ -168,7 +181,7 @@ sub __fork {
}
sub __signal_handler {
warn "[INFO] Caught a signal $OS_ERROR\n";
$log->info ("[INFO] Caught a signal $OS_ERROR");
if (-f $pidfile) {
unlink $pidfile;