3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 11:12:42 +01:00

Logger: reopen logfile after rotation to log final messages

This commit is contained in:
Pragmatic Software 2019-12-30 14:50:36 -08:00
parent 5e40730f36
commit 2a437b479e

View File

@ -9,6 +9,7 @@ use strict;
use feature 'unicode_strings';
use Scalar::Util qw/openhandle/;
use File::Basename;
use Carp ();
@ -48,7 +49,7 @@ sub log {
$text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
if (defined $self->{logfile}) {
if (openhandle *LOGFILE) {
print LOGFILE "$time :: $text";
}
@ -59,8 +60,14 @@ sub rotate_log {
my ($self) = @_;
my $time = localtime $self->{start};
$time =~ s/\s+/_/g;
# logfile has to be closed first for maximum compatibility with `rename`
close LOGFILE;
rename $self->{logfile}, $self->{logfile} . '-' . $time;
# reopen renamed logfile to resume any needed logging
open LOGFILE, ">>$self->{logfile}-$time" or Carp::carp "Couldn't re-open logfile $self->{logfile}-$time: $!\n";
LOGFILE->autoflush(1) if openhandle *LOGFILE;
}
1;