http://qs321.pair.com?node_id=815793

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

I have a perl daemon script which is using Log4perl module to log messages. When the log file contents are cleared (eg: during when the log is rotated), the script is not writing log messages into it.

Note: Log file is given full access priviledge.

Sample script:
#!/usr/local/bin/perl use strict; use warnings; use Log::Log4perl qw(:easy); my $logfile = '/home/y/logs/mylogger.log'; Log::Log4perl->easy_init({ level => $DEBUG, file => ">>$logfile", layout => "%d %F{1}-%L: %m%n" }); my $i = 1; while($i <= 50) { DEBUG "This is a log message"; sleep(10); }
Any pointers on how to resolve this issue ?

Thanks
  • Comment on Log4perl is not writing into the log file when log file contents are cleared
  • Download Code

Replies are listed 'Best First'.
Re: Log4perl is not writing into the log file when log file contents are cleared
by gam3 (Curate) on Jan 05, 2010 at 18:40 UTC
    It is likely that logrotate is unlinking the file. In Unix this does not delete the file, but only the directory entry. Since you keep the file open you keep writing to it. If you are not compressing the file you should be able to tail the logfile.1 file and see that your logging is still going to that file.

    To solve this problem you can either have logrotate do a copytruncate or have you program close (and open) the log when it gets a SIGHUP and set prorotate to send a HUP to your program.

    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: Log4perl is not writing into the log file when log file contents are cleared
by MidLifeXis (Monsignor) on Jan 05, 2010 at 18:41 UTC

    Based on the pathname to the log file, I am guessing this is unixy in nature.

    If the log file is left open by Log4Perl (am not saying it is or isn't), and it is moved out of the way by some external (or even internal process), until the file is reopened by your process, it will continue to write to the old file handle, which may point to the newly renamed file, or a block of bytes on the disk no longer accessible from any directory entry.

    I think that you need to get Log4Perl (or your process) to automatically reopen the file, or....

    rename_log_file(); signal_process_to_reopen_log_file(); wait_for_process_to_reopen_log_file(); compress_or_process_old_log_file();

    --MidLifeXis

Re: Log4perl is not writing into the log file when log file contents are cleared
by Garp (Acolyte) on Jan 05, 2010 at 22:01 UTC

    As part of your log rotation method be sure to send a HUP signal to your perl daemon.

    kill -HUP PID