You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by David Sanders <ds...@chaincast.com> on 2002/10/30 03:05:08 UTC
DailyRollingFileAppender - roll at scheduled time instead of
when record written
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?
http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/DailyRollingFileAppender.html
said:
> 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 day.
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
program.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: DailyRollingFileAppender - roll at scheduled time instead of
when record written
Posted by Shawn Wilson <sh...@atmreports.com>.
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:
http://www.mail-archive.com/log4j-user@jakarta.apache.org/msg06271.html
http://www.mail-archive.com/log4j-user@jakarta.apache.org/msg06354.html
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"/>
</layout>
</appender>
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.
-shawn
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?
>>
>>http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/DailyRollingFileAppender.html
>>said:
>>
>>
>>>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
>>day.
>>
>>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
>>program.
>>
>>
>>
>>
>>
>>
>>--
>>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
====================================
Shawn Wilson [shawnw@atmreports.com]
Software Developer, ATMReports.com
PH: 877-327-0873, FAX: 406-294-5806
====================================
Re: DailyRollingFileAppender - roll at scheduled time instead of when record written
Posted by Fergus Gallagher <Fe...@OrbisUK.com>.
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?
>
> http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/DailyRollingFileAppender.html
> said:
>
> > 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
> day.
>
> 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
> program.
>
>
>
>
>
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
--
Fergus Gallagher Tel: +44 (20) 8742 1600
Orbis Fax: +44 (20) 8742 2649
414 Chiswick High Street email: Fergus.Gallagher@orbisuk.com
London W4 5TL Web: http://www.orbisuk.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>