Limiting log size with log4perl

2019-08-13 00:38发布

I want to limit the size of log files created with log4perl. I don't want my log file to grow to more than about 100mb; I only need the last 100mb (or thereabouts) of data. Ideally, it would truncate earlier data and only keep the last bits. I'm aware of the Log::Dispatch::FileRotate module, but that doesn't quite meet my requirements, as I don't want multiple log files.

Is this possible?

Full code, including inline config, below (minus the use statements):

my $log_conf = q/ 
    log4perl.category = DEBUG, Logfile
    log4perl.appender.Logfile = Log::Dispatch::FileRotate
    log4perl.appender.Logfile.filename = sub { return get_log_fn(); }
    log4perl.appender.Logfile.mode = truncate
    log4perl.appender.Logfile.autoflush = 1 
    log4perl.appender.Logfile.size =  104857600
    log4perl.appender.Logfile.layout    = Log::Log4perl::Layout::PatternLayout     
    log4perl.appender.Logfile.layout.ConversionPattern = %d %P %M(%L) %p %m %n
/;

Log::Log4perl::init( \$log_conf );
my $logger = Log::Log4perl::get_logger();

INFO "Starting $0";
my $upper = 100000000;
for(my $i=0;$i < $upper;$i++) {
    DEBUG $i;
}

sub get_log_fn 
{
    use File::Basename;
    return sprintf "%s.log", basename( $0, '.pl' );     
}

标签: perl log4perl
2条回答
Explosion°爆炸
2楼-- · 2019-08-13 00:44

I had the same problem and found this module:

http://search.cpan.org/dist/Log-Log4perl-Appender-File-FixedSize/lib/Log/Log4perl/Appender/File/FixedSize.pm

Perhaps two years late, but I figured other people who landed here might benefit from this

查看更多
The star\"
3楼-- · 2019-08-13 00:57

I just read a bit and did an experiment. It seems if you leave off the max attribute, keep the size attribute, and use the truncate attribute rather than append while using Log::Dispatch::FileRotate you can get what you want:

#!/usr/bin/env perl

use Modern::Perl;
use Log::Log4perl;

Log::Log4perl::init('./log4perl.conf');

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

for my $num ( 1..1000 ) {
    $logger->debug($num);
}

$logger->debug('blah');

With the associated config file:

###############################################################################
#                              Log::Log4perl Conf                             #
###############################################################################
log4perl.rootLogger              = DEBUG, LOG1
log4perl.appender.LOG1           = Log::Dispatch::FileRotate
log4perl.appender.LOG1.filename  = ./mylog.log
log4perl.appender.LOG1.mode      = truncate
log4perl.appender.LOG1.autoflush = 1
log4perl.appender.LOG1.size      = 1024
#log4perl.appender.LOG1.max       = 5
log4perl.appender.LOG1.layout    = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n

That leaves me with a 130 byte mylog.log with 4 lines in it:

2012/07/25 08:23:14 DEBUG 998
2012/07/25 08:23:14 DEBUG 999
2012/07/25 08:23:14 DEBUG 1000
2012/07/25 08:23:14 DEBUG blah

UPDATE

It looks like FileRotate will always make at least a .1 file even when max is set to 0.

If that absolutely won't work from you, you'll need to write your own apender.

查看更多
登录 后发表回答