我知道如何配置log4j,以便自动轮换日志文件并将日期附加到日志文件中。?
log4j.appender.schedulerService.Directory = ${logdir}
log4j.appender.schedulerService.Prefix = web.log_
log4j.appender.schedulerService.Suffix =
如何使用log4perl实现相同的目标?
我为log4Perl尝试了相同的配置,但看起来像filename参数是必需的。任何帮助表示赞赏。
Mandatory parameter 'filename' missing in call to Log::Dispatch::File::_make_handle
答案 0 :(得分:1)
查看Log::Dispatch::File::Stamped - 它将直接写入日期戳记的日志文件,避免实际旋转它(只要它检测到日期已更改,它就会启动一个新的日志文件)。
log4perl.appender.Logfile=Log::Dispatch::File::Stamped
log4perl.appender.Logfile.min_level=info
log4perl.appender.Logfile.filename=foo.log
log4perl.appender.Logfile.stamp_fmt=%Y%m%d
答案 1 :(得分:0)
这是常见问题: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',
);