Welcome to the Log::Log4perl recipe of the week. Today:
============================================================
Log::Log4perl Recipe of the Week (#9):
How can I roll over my logfiles automatically at midnight?
============================================================
Long-running applications tend to produce ever-increasing
logfiles. For backup and cleanup purposes, however, it is
often desirable to move the current logfile to a different
location from time to time and start writing a new one.
This is a non-trivial task, because it has to happen in sync
with the logging system in order not to lose any messages in
the process.
Luckily, *Mark Pfeiffer*'s "Log::Dispatch::FileRotate"
appender works well with Log::Log4perl to rotate your
logfiles in a variety of ways. All you have to do is specify
it in your Log::Log4perl configuration file and your
logfiles will be rotated automatically.
You can choose between rolling based on a maximum size
("roll if greater than 10 MB") or based on a date pattern
("roll everyday at midnight"). In both cases,
"Log::Dispatch::FileRotate" allows you to define a number
"max" of saved files to keep around until it starts
overwriting the oldest ones. If you set the "max" parameter
to 2 and the name of your logfile is "test.log",
"Log::Dispatch::FileRotate" will move "test.log" to
"test.log.1" on the first rollover. On the second rollover,
it will move "test.log.1" to "test.log.2" and then
"test.log" to "test.log.1". On the third rollover, it will
move "test.log.1" to "test.log.2" (therefore discarding the
old "test.log.2") and "test.log" to "test.log.1". And so
forth. This way, there's always going to be a maximum of 2
saved log files around.
Here's an example of a Log::Log4perl configuration file,
defining a daily rollover at midnight (date pattern
"yyyy-MM-dd"), keeping a maximum of 5 saved logfiles around:
log4perl.category = WARN, Logfile
log4perl.appender.Logfile = Log::Dispatch::FileRotate
log4perl.appender.Logfile.filename = test.log
log4perl.appender.Logfile.max = 5
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd
log4perl.appender.Logfile.TZ = PST
log4perl.appender.Logfile.layout = \
Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %d %m %n
Please see the "Log::Dispatch::FileRotate" documentation for
details. "Log::Dispatch::FileRotate" is available on CPAN.
Have fun! Until next week.
-- Mike
###################################
# Mike Schilli #
# log...@pe... #
# http://perlmeister.com #
# http://log4perl.sourceforge.net #
###################################
|