我们正在尝试用Perl编写一个脚本,该脚本可以生成循环日志。一个程序会生成一个非常大的日志文件,我们想将其分为5、6个左右的文件。
use Log::Dispatch::FileRotate;
my $file = Log::Dispatch::FileRotate->new( name => 'file1',
min_level => 'info',
filename => 'C:TestLog.txt',
dir => 'C:Test',
mode => 'write' ,
size => 5,
max => 6,
);
$file->log( level => 'info', message => 'Comment');
我们在网上找到了这个代码,但它的行为有点不稳定。这个脚本是不是应该在主日志文件增长5MB并最多写入6个文件时写入一个新文件?我们如何才能做到这一点?谢谢
我测试了这段代码,认为size
是以字节为单位的,所以如果你想写5MB的日志,你需要将大小设置为5*1024*1024
或类似的值。我的测试代码,每个写6个文件5MB:
#!/usr/bin/perl
use strict;
use warnings;
use Log::Dispatch::FileRotate;
my $file = Log::Dispatch::FileRotate->new(
name => 'file1',
min_level => 'info',
filename => '/tmp/log/some.log',
mode => 'append',
size => 5*1024*1024,
max => 6,
newline => 1
);
while (1) {
$file->log( level => 'info', message => time );
$file->log( level => 'debug', message => time." debug ".time ); #this will not be logged, because of log level
}
我建议您使用类似Log::Rolling
或Logfile::Rotate
的东西。
这两个模块都是现成的问题解决方案,希望所有的错误都已经解决了。