這是一個常見問題:How can I roll over my logfiles automatically at midnight?
子類Log::Dispatch::FileRotate添加自定義的文件擴展名:
package Log::Dispatch::FileRotate::FileExtension;
use parent 'Log::Dispatch::FileRotate';
use strictures;
use SUPER qw();
use Time::Piece qw();
sub new {
my ($self, %p) = @_;
$self = $self->SUPER(%p);
$self->{extension} = $p{extension};
return $self;
}
sub log_message {
### lines 177..235 from parent class go here
warn localtime() . " $$ Rotating\n" if $self->{debug};
my $stamp = Time::Piece->localtime->strftime($self->{extension});
warn "$$ rename $name $stamp\n" if $self->{debug};
rename $name, sprintf('%s.%s', $name, $stamp);
warn localtime() . " $$ Rotating Done\n" if $self->{debug};
### lines 257..266 from parent class go here
}
1;
用法:
use Log::Dispatch::FileRotate::FileExtension qw();
my $file = Log::Dispatch::FileRotate::FileExtension->new(
name => 'file1',
min_level => 'info',
filename => 'Somefile.log',
DatePattern => 'yyyy-MM-dd',
extension => '%F',
);
我已經看到了這種結構,旋轉日誌文件等創建的文件web.log_,web.log_.1,web_log_.2,而我需要的文件名包含日期以及(網絡.log_2012-07-15,web.log_2012-07-16 etc ...) – Sriharsha 2012-07-24 07:02:33
'DatePattern'也在常見問題中解釋。請仔細閱讀。 – daxim 2012-07-24 07:16:07
DatePattern定義*何時旋轉*,並且不影響 的文件擴展名... [此處](http://osdir.com/ml/lang.perl.modules.log4perl.devel/2007-02/msg00004 .html) – Sriharsha 2012-07-26 06:59:53