1

I know how to configure the log4j so that the logs file gets rotated automatically with the date appended to the log file.?

log4j.appender.schedulerService.Directory = ${logdir}
log4j.appender.schedulerService.Prefix = web.log_
log4j.appender.schedulerService.Suffix = 

How can I achieve the same with log4perl?

I tried the same config for log4Perl, but it looks like for filename parameter is mandatory. Any help is appreciated.

Mandatory parameter 'filename' missing in call to Log::Dispatch::File::_make_handle
Sriharsha
  • 2,373
  • 1
  • 16
  • 20

2 Answers2

1

Have a look at Log::Dispatch::File::Stamped - it will write directly to a datestamped logfile, avoiding the need to actually rotate it (it will just start a new logfile whenever it detects the date has changed).

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
andytech
  • 211
  • 1
  • 4
0

This is a FAQ: How can I roll over my logfiles automatically at midnight?


Subclass Log::Dispatch::FileRotate to add a custom file extension:

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;

Usage:

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',
);
daxim
  • 39,270
  • 4
  • 65
  • 132
  • I already saw that configuration, it rotates the log files and creates the files web.log_, web.log_.1 , web_log_.2 so on, whereas I need the filenames to contain the Dates as well (web.log_2012-07-15, web.log_2012-07-16 etc...) – Sriharsha Jul 24 '12 at 07:02
  • The `DatePattern` is explained in the FAQ as well. Do read it carefully. – daxim Jul 24 '12 at 07:16
  • 1
    The DatePattern defines *when to rotate*, and does not affect the file extension...[here](http://osdir.com/ml/lang.perl.modules.log4perl.devel/2007-02/msg00004.html) – Sriharsha Jul 26 '12 at 06:59