ateague has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE:
The problem was caused by the version of Log-Dispatch-FileRotate I was using. Version 1.11 does not rotate files as expected. The more recent 1.19 version does not have that problem. (When I originally started working with Log-Dispatch-FileRotate it was not available on ActiveState's PPM repository, so I downloaded and installed it from Log4perl's SourceForge repository.)



Good morning!

I am writing a script that writes to log files that are rotated every day (maximum of 8). I am using Log::Log4perl and Log::Dispatch::FileRotate and I am running into some problems (or possibly misunderstandings?).

I am attempting to rotate the log files everyday with a date pattern string of yyyy-MM-dd. This works properly for scripts that run continuously longer than a day, but does not appear to work as expected for shorter running scripts (e.g. script starts up at 20:30 2015-01-14, runs for 5 minutes and exits, then runs again at 02:30 2015-01-15; the log file does not get rotated when the script runs on a new day).

Perl info:

perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x +64-multi-thread

Module info:

ppm list --no-header --matching Log- --fields name,version Log-Dispatch 2.42 Log-Dispatch-FileRotate 1.11 Log-Log4perl 1.46 Log-Message 0.08 Log-Message-Simple 0.10

Below are two small example programs that rotate the log file every minute.
Runs continuously for a minute+1 second and rotates as expected:

#!/usr/bin/perl use 5.018; use Log::Dispatch::FileRotate; use Log::Log4perl qw/:easy :no_extra_logdie_message/; use strict; use warnings; local $/ = undef; Log::Log4perl::init(\<DATA>); local $| = 1; INFO("LOG 1: Sleep for 61 seconds"); for ( 1 .. 61 ) { print "\b\b$_"; sleep 1; } INFO("LOG 2: Done"); 1; __DATA__ # Layout for logging # [Date] [Priority] [File::Caller] [Line Number] > Message \n layout_pattern = [%d{yyyy-MM-ddTHH:mm:ss}] [%p] [%F{1}::%M] [%L] > %m +%n ## ## LOGGERS ## log4perl.logger = INFO, LogRotate # Logging to file # - Rotates every minute # - Last 8 minutes kept # (Only INFO level priority and higher messages are logged to file) log4perl.appender.LogRotate.layout.ConversionPattern = ${layout_patter +n} log4perl.appender.LogRotate = Log::Dispatch::FileRotate log4perl.appender.LogRotate.filename = Timed_Rotate.log log4perl.appender.LogRotate.layout = Log::Log4perl::Layout::Patte +rnLayout log4perl.appender.LogRotate.max = 8 log4perl.appender.LogRotate.mode = append log4perl.appender.LogRotate.DatePattern = yyyy-MM-dd-HH-MM

Called every minute+1 second and does not rotate the log file as expected:

#!/usr/bin/perl use 5.018; use Log::Dispatch::FileRotate; use Log::Log4perl qw/:easy :no_extra_logdie_message/; use strict; use warnings; local $/ = undef; Log::Log4perl::init(\<DATA>); local $| = 1; INFO("LOG 1: Sleep for 61 seconds"); 1; __DATA__ # Layout for logging # [Date] [Priority] [File::Caller] [Line Number] > Message \n layout_pattern = [%d{yyyy-MM-ddTHH:mm:ss}] [%p] [%F{1}::%M] [%L] > %m +%n ## ## LOGGERS ## log4perl.logger = INFO, LogRotate # Logging to file # - Rotates every minute # - Last 8 minutes kept # (Only INFO level priority and higher messages are logged to file) log4perl.appender.LogRotate.layout.ConversionPattern = ${layout_patter +n} log4perl.appender.LogRotate = Log::Dispatch::FileRotate log4perl.appender.LogRotate.filename = Timed_Rotate.log log4perl.appender.LogRotate.layout = Log::Log4perl::Layout::Patte +rnLayout log4perl.appender.LogRotate.max = 8 log4perl.appender.LogRotate.mode = append # 'log4j' style doesn't work as expected log4perl.appender.LogRotate.DatePattern = yyyy-MM-dd-HH-MM # ... neither does 'Date::Manip' style # log4perl.appender.LogRotate.DatePattern = 0:0:0:0:0:1:0

Called (assuming code was saved in a file called "test.pl")via: perl -E "local $|=1;`test.pl`; for(1..60){print qq|\b\b$_|;sleep 1;} `test.pl`;"

I am not quite sure what is going on here. Did I do something wrong? Do I have a flawed understanding of how Log::Dispatch::FileRotate is suposed to work?

Thank you for your time.

  • Comment on [SOLVED] Log::Log4perl and Log::Dispatch::FileRotate: Issues rotating log files based on date pattern
  • Select or Download Code

Replies are listed 'Best First'.
Re: Log::Log4perl and Log::Dispatch::FileRotate: Issues rotating log files based on date pattern (rotate log every day)
by Anonymous Monk on Jan 27, 2015 at 00:53 UTC
    The easiest way to rotate the log every day is to use Re: log4Perl dynamic filename its FAQ
    log4perl.appender.Logfile.filename = sub { my $date = POSIX::strftime('%Y-%m-%d', localtime ); return "myapp-$date.log"; };

      Thanks.

      That certainly will create new log files every day but, the downside to that approach is that it will not *rotate* the log files after a certain number of days (8 in my case)

Re: Log::Log4perl and Log::Dispatch::FileRotate: Issues rotating log files based on date pattern
by Arunbear (Prior) on Jan 25, 2015 at 15:29 UTC
    Sorry for answering your question with a question, but since you seem to be running on *nix, is there any reason for not using your system's logrotate tool? At $work we use logrotate not just with Log4perl, it also handles our Java apps, Apache, MySQL etc.
      but since you seem to be running on *nix, is there any reason for not using your system's logrotate tool?

      Errr...
      I am actually on Windows, using ActivePerl 5.18.

        Apologies, I saw the
        #!/usr/bin/perl
        in your code, and missed the part where you mentioned Windows.
Re: Log::Log4perl and Log::Dispatch::FileRotate: Issues rotating log files based on date pattern
by Anonymous Monk on Jan 26, 2015 at 10:57 UTC