Open ikegami opened 4 years ago
This ticket was filed in response to the following question on SO: https://stackoverflow.com/q/64111885/589924
Workaround:
sub post_rotate {
my (undef, $index, $fileRotate) = @_;
# Current path to the rotated file.
my $current_qfn = $fileRotate->filename() . "." . ($index+1);
# Handle the situation where this callback is called too many times.
return if !-e $current_qfn;
...
}
I have applied your test programs to the most recent version 1.38 of Log::Dispatch::FileRotate and I cannot confirm all your findings.
$filename
(the first parameter passed to the post_rotate
callback) all the time exists as a physical file.max => 1
means that there should be no rotated file: $filename == $fileRotate->filename == problem2.log
is ok but problem2.log.1 should not exist although it does. In general there exists always one more file with the index max
that should not exist.Deleted a comment. It's not quite right.
I have applied your test programs to the most recent version 1.38 of Log::Dispatch::FileRotate and I cannot confirm all your findings.
The programs still output exactly the same result. The problems still exist.
- problem1.pl:
$filename
(the first parameter passed to thepost_rotate
callback) all the time exists as a physical file.
The problem isn't that the file doesn't exist when post_rotate was called. The problem is that post_rotate is sometimes called more times than the number of rotations.
With max => 3,
This is harmful, and in direct contradiction of the documentation ("Will be called one time for every rotated file")
- problem2.pl:
max => 1
means that there should be no rotated file:
That's not relevant. The same lack of information exists regardless of the value of max
.
For example, make these changes to problem2.pl:
max => 2
$log->info("x" x $rotate_size);
$log->info("x");
say "--";
$log->info("x" x $rotate_size);
$log->info("x");
The output is now:
...
--
[problem2.log.1][problem2.log]
[problem2.log][problem2.log]
It would be nice if we didn't have to calculate the new name ourselves.
Sorry I wasn't clearer.
This report brings up two issues with a common solution. Suggested solution at the bottom.
post_rotate
is called an inconsistent number of timespost_rotate
is called for files that weren't rotated.post_rotate
is only called for files that were rotated.Test program and output:
post_rotate
's usefulness is neuteredThe callback is provided the original name of the rotated file, but it would be nice if it was also provided the new name of the file.
Currently, the documentation claims the first parameter is "the path of the rotated file", which unclear. That could mean
The first parameter is the former path of the rotated file. This is not the interpretation I find the most likely, and it's the least useful of the two options. In fact, the former bit of information (the new name of the rotated file) is not provided at all. It would be useful to have this.
Test program and output:
Suggested Solution
In order to preserver backwards compatibility, I propose the following solution:
Remove the following code from
rotate
:Add the following code to
_move_file
:In any case, the documentation for the
$filename
parameter needs fixing.