logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shawn Wilson <sha...@atmreports.com>
Subject Re: DailyRollingFileAppender - roll at scheduled time instead of when record written
Date Wed, 30 Oct 2002 17:45:41 GMT
As it turns out I have implemented option 1 you listed, though for a 
different reason. However, I think it may still serve David's needs. I 
implemented this functionality because the call to Java.rename() would 
fail every once in a while for no apparent reason. See:


I've attached a DailyRollingFileAppender.java file that I modified from 
the log4j-1.2.7 source. Basically it should be backwards compatible to 
the distributed version without changes to your log4j.xml file. However, 
if you want to use the feature I added you would set the new AlwaysDated 
parameter to "true".

Here is an example:

     <appender name="A3" class="org.apache.log4j.DailyRollingFileAppender">
         <param name="DatePattern" value="'.'yyyy-MM-dd"/>
         <param name="AlwaysDated" value="true"/>
         <param name="File" value="Scheduler.log"/>
         <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%-15d{ISO8601} %-10p 
(%c) - %m%n"/>

By the way... I make no guarantees to the attached code. I have 
minimally tested it and it seems to work but I would recommend you do 
your own testing if you decide to use it.


Fergus Gallagher wrote:
> DailyRollingFileAppender won't do this in the present incarnation.  It would
> require a seperate thread.
> Options I can think of:
> 	1) modify DRFA to write the log with the date extension directly, so
> it's always available.
> 	2) (if you have unix) use a simple FileAppender use logrotate from a cronjob. 
> You would need to use the 'copytruncate' option which can cause a small
> dataloss
> 	3)  use ExternallyRolledFileAppender and get a cron job to trigger
> that.  This uses numerical versioning, I think so possibly not suitable.
> 	4) Use a Timer/TimerTask to write a log message at midnight (or
> maybe 1s past, to be safe!)
> (4) is certainly the easiest.....
> Fergus
> ASIDE: I generally log to ConsoleAppender which is redirected by the OS. I
> prefer this so I can get output from parts of the application that don't use
> Log4J (3rd party libs, JVM stacktraces, etc)
> On Tue, Oct 29, 2002 at 06:05:08PM -0800, David Sanders wrote:
>>With DailyRollingFileAppender, how do I cause files to be rolled over on 
>>the configurable interval instead of waiting for the first log write 
>>attempt after the interval expires?

>>>DailyRollingFileAppender extends FileAppender so that the underlying 
>>file is rolled over at a user chosen frequency.
>>>For example, if the File option is set to /foo/bar.log and the 
>>DatePattern set to '.'yyyy-MM-dd, on 2001-02-16 at midnight, the logging 
>>file >/foo/ bar.log will be copied to /foo/bar.log.2001-02-16 and logging 
>>for 2001-02-17 will continue in /foo/bar.log until it rolls over the next 
>>The file won't be rolled on 2001-02-16 at midnight unless a message is 
>>logged at midnight.  Instead, the file will be rolled when the first log 
>>message is written after midnight.  In my application, that "first message" 
>>could be several hours after midnight.  Yet, I need the log file closed 
>>(and preferably renamed) at midnight so the log can be processed by another 
>>To unsubscribe, e-mail:   <mailto:log4j-user-unsubscribe@jakarta.apache.org>
>>For additional commands, e-mail: <mailto:log4j-user-help@jakarta.apache.org>

Shawn Wilson [shawnw@atmreports.com]
Software Developer, ATMReports.com
PH: 877-327-0873, FAX: 406-294-5806

View raw message