Subject: | if lockfile can't be opened, nothing is logged but no failure is reported |
If the lockfile becomes unwriteable, for instance if some other process
owned by another user makes it 0600, then nothing is logged but no error
is reported. In a large system the problem can be really hard to isolate.
Attached is a patch that at least warns to STDERR if that happens, with
a simplistic test that at least demonstrates the behavior.
Subject: | ldfr.diff |
diff -Naur Log-Dispatch-FileRotate-1.19.orig/FileRotate.pm Log-Dispatch-FileRotate-1.19/FileRotate.pm
--- Log-Dispatch-FileRotate-1.19.orig/FileRotate.pm 2008-10-20 16:32:07.000000000 -0700
+++ Log-Dispatch-FileRotate-1.19/FileRotate.pm 2010-12-02 10:15:45.444431221 -0800
@@ -191,7 +191,7 @@
warn "$$ waiting on lock\n" if $self->{debug};
unless($self->lfhlock())
{
- warn "$$ failed to get lock. returning\n" if $self->{debug};
+ warn qq{$$ LogRotate failed to get lock: "$self->{_lfhlock_test_err}". Not logging...\n};
return;
}
warn "$$ got lock after wait\n" if $self->{debug};
@@ -611,6 +611,7 @@
}
else
{
+ $self->{_lfhlock_test_err} = "couldn't lock $self->{lf}: $!";
$self->{lfh} = 0;
warn "$$ couldn't get lock on Lock File\n" if $self->{debug};
return 0;
diff -Naur Log-Dispatch-FileRotate-1.19.orig/test.pl Log-Dispatch-FileRotate-1.19/test.pl
--- Log-Dispatch-FileRotate-1.19.orig/test.pl 2008-10-20 16:32:07.000000000 -0700
+++ Log-Dispatch-FileRotate-1.19/test.pl 2010-12-02 10:17:12.260844470 -0800
@@ -8,7 +8,7 @@
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
-BEGIN { $| = 1; print "1..8\n"; }
+BEGIN { $| = 1; print "1..9\n"; }
END {print "not ok 1\n" unless $loaded;}
use Log::Log4perl;
use Log::Dispatch::FileRotate;
@@ -176,4 +176,44 @@
}
print "ok 8\n";
+
+############################
+# lockfile handling
+
+my $lockfile_test = 'testlock.log';
+unlink $lockfile_test;
+$config = <<EOL;
+log4perl.logger = INFO, default
+log4perl.appender.default = Log::Dispatch::FileRotate
+log4perl.appender.default.filename = $lockfile_test
+log4perl.appender.default.mode = append
+log4perl.appender.default.layout = SimpleLayout
+log4perl.appender.default.max = 6
+log4perl.appender.default.DatePattern = yyyy-MM-dd-HH
+EOL
+
+Log::Log4perl::init(\$config);
+
+$logger->info("write with successful lock");
+
+# look up the lockfile name
+my $appender = Log::Log4perl->appender_by_name('default');
+my $lockfile = $appender->{lf}; # .testlock.log.LCK
+
+# somebody borks the lockfile
+chmod 0, $lockfile;
+
+# this should throw a warning
+$logger->info("where's my log?");
+
+# if the string actually showed up in the log file, then our assumptions are wrong
+open (my $fh, $lockfile_test);
+my $got = do { local $/ = undef; <$fh> };
+if ($got =~ /where's my log/){
+ print "not ";
+}
+print "ok 9";
+
+unlink $lockfile_test;
+
unlink $_ for <test.log*>;