[log4perl-devel] File rotation every day

2008-01-16 Thread luc.belleville
Hi all,

I am new with log4perl.

 

I am testing it and it looks like it is working OK.  I am wondering if it is 
possible to do what I want to with it.  I want, when the file rotate every day 
to change the file name to include the date in the file name?

 

My config file is like that :

 

layout_class = Log::Log4perl::Layout::PatternLayout

layout_pattern = %d %p> %F{1}:%L %M - %m%n

 

log4perl.logger = FATAL, Screen

log4perl.logger.Groceries.Food = DEBUG, Log

 

log4perl.appender.Screen = Log::Dispatch::Screen

log4perl.appender.Screen.stderr = 0

log4perl.appender.Screen.Threshold = DEBUG

log4perl.appender.Screen.layout = ${layout_class}

log4perl.appender.Screen.layout.ConversionPattern = ${layout_pattern}

 

log4perl.appender.Log = Log::Dispatch::FileRotate

log4perl.appender.Log.filename = sub { return getLogFilename(); }

log4perl.appender.Log.mode = append

log4perl.appender.Log.DatePattern = -MM-dd

log4perl.appender.Log.max = 2

log4perl.appender.Log.layout = ${layout_class}

log4perl.appender.Log.layout.ConversionPattern = ${layout_pattern}

 

and the getLogFilename is a sub that format the file name like MMdd.test.log

 

But like I read in the documentation, the sub are executed only once at the 
time the config file is read.  So the date in the file name is always the date 
when I start the program.

 

 

Is there a way to roll the file and changing the file name at roll time?

 

Thank,

__

Belleville, Luc

Analyste principal - gestion de système

Bell Solutions d'impartition en TIC

505 de Maisonneuve O., 1er étage

Montréal, Québec, H3A 3C2

Tel : (514) 499-3512

Fax : (514) 499-3577

Pagette : (514) 749-8045

[EMAIL PROTECTED]  

 

 

 

<>-
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/___
log4perl-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/log4perl-devel


Re: [log4perl-devel] File rotation every day

2008-01-16 Thread Mike Schilli
On Wed, 16 Jan 2008 [EMAIL PROTECTED] wrote:

> I am testing it and it looks like it is working OK.  I am wondering if
> it is possible to do what I want to with it.  I want, when the file
> rotate every day to change the file name to include the date in the
>
> But like I read in the documentation, the sub are executed only once
> at the time the config file is read. So the date in the file name is
> always the date when I start the program.

That's correct, only the appender or an external program can take care
of renaming the log file.

Log::Dispatch::FileRotate (which is not part of Log4perl but a 3rd party
module) renames log files as file.log.1, file.log.2, etc., but that's
not what you want.

Instead, I'd suggest you use an external rotator and let it take care of
the renaming issue. Make sure you read the gotchas regarding external
rotators, first:

http://log4perl.sourceforge.net/d/Log/Log4perl/FAQ.html#2d0d0

Then I'd probably use a configuration like this:

  log4perl.category = WARN, Logfile
  log4perl.appender.Logfile  = Log::Log4perl::Appender::File
  log4perl.appender.Logfile.filename = test.log
  log4perl.appender.Logfile.recreate = 1
  log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout
  log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m%n

and an external rotator that kicks in right after midnight every day as
a cronjob:

  01 00 * * * cd /my/logs; mv test.log test.log.`date +%F`

Because of the 'recreate' flag set in the Log4perl configuration,
Log4perl will notice when the file is missing (until it does notice, it will
keep writing to the moved file, which is fine if you hold off on
compressing it until the switch is made).

The Log::Log4perl::Appender::File documentation lists more details on
the 'recreate' flag.

Hope that helps!

-- Mike

Mike Schilli
[EMAIL PROTECTED]

-
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/
___
log4perl-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/log4perl-devel