0

下面的代码是这样的:每当日志文件大小超过 5 MB 时,创建一个名为 , 等的新日志logfile.log.1文件logfile.log.2

但是如何logfile_20140530_15_31.log使用log4perl模块创建我们自己的日志文件名?

my $log_conf = "
   log4perl.rootLogger              = DEBUG, LOG1

   log4perl.appender.LOG1           = Log::Dispatch::FileRotate
   log4perl.appender.LOG1.filename  = $logfile.log
   log4perl.appender.LOG1.mode      = append
   log4perl.appender.LOG1.autoflush = 1
   log4perl.appender.LOG1.size      = 1024000
   log4perl.appender.LOG1.max       = 5
   log4perl.appender.LOG1.layout    = Log::Log4perl::Layout::PatternLayout
   log4perl.appender.LOG1.layout.ConversionPattern = \%d{yyyy-MM-dd HH:mm:ss}\%P|\%p|\%m|\%n
";

Log::Log4perl::init(\$log_conf);

my $logger = Log::Log4perl->get_logger();

$logger->info("error ");
4

1 回答 1

0

从 Log::Dispatch::FileRotate 的文档中:

SYNOPSIS

  use Log::Dispatch::FileRotate;

  my $file = Log::Dispatch::FileRotate->new( name      => 'file1',
                                       min_level => 'info',
                                       filename  => 'Somefile.log',
                                       mode      => 'append' ,
                                       size      => 10,
                                       max       => 6,
                                      );
  # or for a time based rotation

  my $file = Log::Dispatch::FileRotate->new( name      => 'file1',
                                       min_level => 'info',
                                       filename  => 'Somefile.log',
                                       mode      => 'append' ,
                                       TZ        => 'AEDT',
                                       DatePattern => 'yyyy-dd-HH',
                                      );

  $file->log( level => 'info', message => "your comment\n" );

也许您可以将size&max替换为TZ& DatePattern

于 2014-05-30T13:36:25.057 回答