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' );
}
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
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:
With the associated config file:
That leaves me with a 130 byte mylog.log with 4 lines in it:
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.