3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

Logger: rename log file to log-<start-up timestamp> at normal termination

This commit is contained in:
Pragmatic Software 2019-12-29 11:06:52 -08:00
parent 4fbdcd948f
commit 54a661955b
2 changed files with 27 additions and 19 deletions

View File

@ -19,24 +19,26 @@ sub new {
my ($class, %conf) = @_;
my $logfile = delete $conf{filename};
my $pbot = $conf{pbot} // Carp::croak "Missing pbot reference to " . __FILE__;
my $logfile = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__;
if (defined $logfile) {
my $path = dirname $logfile;
if (not -d $path) {
print "Creating new logfile path: $path\n";
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
}
open LOGFILE, ">>$logfile" or Carp::croak "Couldn't open logfile $logfile: $!\n";
LOGFILE->autoflush(1);
my $path = dirname $logfile;
if (not -d $path) {
print "Creating new logfile path: $path\n";
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
}
my $self = {
logfile => $logfile,
};
open LOGFILE, ">>$logfile" or Carp::croak "Couldn't open logfile $logfile: $!\n";
LOGFILE->autoflush(1);
my $self = bless {
logfile => $logfile,
pbot => $pbot,
start => time,
}, $class;
$self->{pbot}->{atexit}->register(sub { $self->rotate_log; return; });
bless $self, $class;
return $self;
}
@ -53,4 +55,12 @@ sub log {
print "$time :: $text";
}
sub rotate_log {
my ($self) = @_;
my $time = localtime $self->{start};
$time =~ s/\s+/_/g;
close LOGFILE;
rename $self->{logfile}, $self->{logfile} . '-' . $time;
}
1;

View File

@ -54,8 +54,9 @@ sub new {
my ($class, %conf) = @_;
my $self = bless {}, $class;
$self->initialize(%conf);
$self->{atexit} = PBot::Registerable->new(%conf);
$self->register_signal_handlers;
$self->initialize(%conf);
return $self;
}
@ -80,7 +81,7 @@ sub initialize {
}
# logger created first to allow other modules to log things
$self->{logger} = PBot::Logger->new(filename => "$data_dir/log/log", %conf);
$self->{logger} = PBot::Logger->new(pbot => $self, filename => "$data_dir/log/log", %conf);
$self->{version} = PBot::VERSION->new(pbot => $self, %conf);
$self->{logger}->log($self->{version}->version . "\n");
@ -88,12 +89,9 @@ sub initialize {
return if $conf{logger_only};
$self->{atexit} = PBot::Registerable->new(%conf);
$self->{timer} = PBot::Timer->new(timeout => 10, %conf);
$self->{commands} = PBot::Commands->new(pbot => $self, %conf);
$self->{func_cmd} = PBot::FuncCommand->new(pbot => $self, %conf);
$self->{refresher} = PBot::Refresher->new(pbot => $self);
# make sure the environment is sane