Log::D...eRotate(3pm)
π¦ Log::Dispatch::FileRotate π
okay bestie, this is the manual page for the perl module Log::Dispatch::FileRotate (that's the 3pm one), and it's a whole vibe. it's long, so buckle up β we're doing this in two parts. πΈ
β¨ NAME β¨
Log::Dispatch::FileRotate β Log to Files that Archive/Rotate Themselves π₯
π VERSION π
version 1.38 β no cap, that's the number.
π SYNOPSIS π
here's the tea, in code:
use Log::Dispatch::FileRotate;
my $logger = Log::Dispatch::FileRotate->new(
name => 'file1',
min_level => 'info',
filename => 'Somefile.log',
mode => 'append' ,
size => 10*1024*1024,
max => 6);
# or for a time based rotation
my $logger = Log::Dispatch::FileRotate->new(
name => 'file1',
min_level => 'info',
filename => 'Somefile.log',
mode => 'append' ,
TZ => 'AEDT',
DatePattern => 'yyyy-dd-HH');
# and attach to Log::Dispatch
my $dispatcher = Log::Dispatch->new;
$dispatcher->add($logger);
$dispatcher->log( level => 'info', message => "your comment\n" );
look at that second one β that's the time based rotation glow up β¨
π DESCRIPTION π
so this module extends the base class Log::Dispatch::Output so it can provide a simple object for logging to files inside the whole Log::Dispatch::* system, and it rotates them automatically according to different constraints. basically it's a Log::Dispatch::File wrapper with extras. it's giving "the original, but with main character energy." π
π Rotation π
there are three different constraints that decide when a file has to be rotated.
- the first one is by size: when the log file grows more than a specified size, then it's rotated.
- the second constraint is with occurrences. if a
DatePatternis defined, a file rotation ignores the size constraint (unlesscheck_both) and uses the defined date pattern constraints instead. when you're usingDatePatternmake sure TZ is defined correctly, and make sure the TZ you use is actually understood byDate::Manip. we useDate::Manipto generate our recurrences. bad TZ equals bad recurrences equals surprises! π go read theDate::Manipman page for more details on TZ.DatePatternwill default to a daily rotate if your entered pattern is incorrect. you will also get a warning message. - you can also check both constraints together by using the
check_bothparameter.
and the last constraint is a user callback. this function gets called outside the restricted area (peep "Concurrency" below) and, if it returns a true value, a rotation will happen unconditionally.
all checks are made before logging. the rotate method lets us check these constraints without logging anything, which is iconic.
and to give the user even more power, there's a post_rotate callback that gets called after every rotation. π«Ά
π Concurrency π
multiple writers are allowed by this module. there's a restricted area where only one writer can be inside at a time. this is done by using an external lock file, which is named ".filename.LCK" β and no, it is never deleted. π
the user constraint and the DatePattern constraint are both checked outside this restricted area. so, when you write a callback, don't rely on the logging file, because it can disappear right under your feet. π
within this restricted area we:
- check the size constraint
- eventually rotate the log file
- if it's defined, call the
post_rotatefunction - write the log message
periodt. β¨
π§ METHODS π§
new(%p)
the constructor takes the following parameters in addition to the parameters documented in Log::Dispatch::File:
max ($)β the maximum number of log files to create. default1.size ($)β the maximum (or close to) size the log file can grow to. default10M.DatePattern ($)β theDatePatternas defined above.TZ ($)β the TimeZone time based calculations should be done in. this should matchDate::Manip's concept of timezones and, of course, your machine's timezone.check_both ($)β1for checkingDatePatternand size concurrently,0otherwise. default0.user_constraint (\&)β if this callback is defined and returns true, a rotation will happen unconditionally.post_rotate (\&)β this callback is called after all files were rotated. it'll be called one time for every rotated file (in reverse order) with these arguments:"filename"β the path of the rotated file"index"β the index of the rotated file frommax-1 to0, and in the latter casefilenameis the new, empty, log file"fileRotate"β an object reference to this instance
with this, you can have infinite files renaming each time the rotated file logs. e.g:
my $file = Log::Dispatch::FileRotate
->new(
...
post_rotate => sub {
my ($filename, $idx, $fileRotate) = @_;
if ($idx == 1) {
use POSIX qw(strftime);
my $basename = $fileRotate->filename();
my $newfilename =
$basename . '.' . strftime('%Y%m%d%H%M%S', localtime());
$fileRotate->debug("moving $filename to $newfilename");
rename($filename, $newfilename);
}
},
);
Note: this is called within the restricted area (go look at"Concurrency"again). that means any other concurrent process is locked in the meanwhile. and for the exact same reason, don't use thelog()orlog_message()methods, because you will get a deadlock! ππ¨
DEBUG ($)β turn on lots of warning messages toSTDERRabout what this module is doing, if set to1. really only useful to me. π
filename()
returns the log filename. simple. slay. π
setDatePattern( $ or [ $, $, ... ] )
set a new suite of recurrances for file rotation. you can pass in a single string or a reference to an array of strings. multiple recurrences can also be defined within a single string by separating them with a semi-colon (;).
see the discussion above regarding the setDatePattern paramater for more details. π
log_message( message => $ )
sends a message to the appropriate output. generally this shouldn't be called directly β it should be called through the log() method (in Log::Dispatch::Output). iykyk. β¨
rotate()
rotates the file, if it has to be done. you can call this method if you want to check β and eventually do β a rotation without logging anything.
it returns 1 if a rotation was done, 0 otherwise. and undef on error. π
debug($)
if DEBUG is true, it prints a standard warning message.
π‘ Tip π‘
if you have multiple writers that were started at different times, you'll find each writer will try to rotate the log file at a recurrence calculated from its own start time. to sync all the writers, just use a config file and update it after starting your last writer. this will cause new() to be called by each of the writers close to the same time, and if your recurrences aren't too close together, all should sync up just nicely. π«Ά
i initially assumed a long running process, but it seems people are using this module as part of short running CGI programs. so, now we look at the last modified time stamp of the log file and compare it to a previous occurance of a DatePattern β on startup only. if the file stat shows the mtime to be earlier than the previous recurrance, then i rotate the log file. πΈ
π DatePattern π
as i said earlier, we use Date::Manip for generating our recurrence events. this means we can understand Date::Manip's recurrence patterns and the normal log4j DatePatterns. we don't use DatePattern to define the extension of the log file though. π
DatePattern can therefore take forms like:
- Date::Manip style
0:0:0:0:5:30:0β every 5 hours and 30 minutes0:0:0:2*12:30:0β every 2 days at 12:30 (each day)3*1:0:2:12:0:0β every 3 years on Jan 2 at noon
- DailyRollingFileAppender log4j style
yyyy-MMβ every monthyyyy-wwβ every weekyyyy-MM-ddβ every dayyyyy-MM-dd-aβ every day at noonyyyy-MM-dd-HHβ every houryyyy-MM-dd-HH-MMβ every minute
to specify multiple recurrences in a single string, separate them with a semicolon:
yyyy-MM-dd; 0:0:0:2*12:30:0
this says we want to rotate every day AND every 2 days at 12:30. put in as many as you like. π₯
a complete description of Date::Manip recurrences is beyond us here, except to quote (from the man page):
A recur description is a string of the format
Y:M:W:D:H:MN:S . Exactly one of the colons may
optionally be replaced by an asterisk, or an asterisk
may be prepended to the string.
Any value "N" to the left of the asterisk refers to
the "Nth" one. Any value to the right of the asterisk
refers to a value as it appears on a calendar/clock.
Values to the right can be listed a single values,
ranges (2 numbers separated by a dash "-"), or a comma
separated list of values or ranges. In a few cases,
negative values are appropriate.
This is best illustrated by example.
0:0:2:1:0:0:0 every 2 weeks and 1 day
0:0:0:0:5:30:0 every 5 hours and 30 minutes
0:0:0:2*12:30:0 every 2 days at 12:30 (each day)
3*1:0:2:12:0:0 every 3 years on Jan 2 at noon
0:1*0:2:12,14:0:0 2nd of every month at 12:00 and 14:00
1:0:0*45:0:0:0 45th day of every year
0:1*4:2:0:0:0 4th tuesday (day 2) of every month
0:1*-1:2:0:0:0 last tuesday of every month
0:1:0*-2:0:0:0 2nd to last day of every month
π TODO π
- compression
- signal based rotates
- proper test suite
could possibly use Logfile::Rotate as well/instead. π€
π SEE ALSO π
Log::Dispatch::File::Stamped
Log directly to timestamped files.
π HISTORY π
originally written by Mark Pfeiffer, <markpf at mlp-consulting dot com dot au>, inspired by Dave Rolsky's <autarch at urth dot org> code :-) β we stan a collaborative origin story. π«Ά
Kevin Goess <cpan at goess dot org> suggested that multiple writers should be supported. he also conned me into doing the time based stuff. Thanks Kevin! :-) π
thanks also to Dan Waldheim for helping with some of the locking issues in a forked environment.
and thanks to Stephen Gordon for his more portable code on lockfile naming.
π SOURCE π
the development version is on github at <https://https://github.com/mschout/perl-log-dispatch-filerotate> and may be cloned from <git://https://github.com/mschout/perl-log-dispatch-filerotate.git>
π BUGS π
please report any bugs or feature requests on the bugtracker website <https://github.com/mschout/perl-log-dispatch-filerotate/issues>
when submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. that's the bare minimum, bestie. β¨
π AUTHOR π
Michael Schout <mschout@cpan.org>
okay that's the first chunk of the manual page β but we're not done yet, more parts are coming. π πΈ
π COPYRIGHT AND LICENSE π
okay bestie, legal era, but make it cute π
This software is copyright (c) 2005 by Mark Pfeiffer. yes, 2005, we are talking vintage, an iconic elder of the Perl ecosystem π
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. so like, the license is literally just "same energy as Perl 5" π no cap, that's the whole deal.
tl;dr bestie: it's free, it's Perl-5-licensed, and it's been free since 2005. go forth and redistribute responsibly π«Ά
and that's a wrap on Log::D...eRotate(3pm) πβ¨ you made it allll the way to the bottom, which is honestly main character energy fr fr. this page came from perl v5.40.1, dated 2025-09-20, and if you ever forget any of the gorgeous chaos above, your bestie man is always one q away from letting you read it again π go forth, rotate your logs, and slay π
π¦ where this page comes from
- ubuntu resolute: liblog-dispatch-filerotate-perl 1.38-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: liblog-dispatch-filerotate-perl 1.38-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms