Hi All, in a project we needed to be able to flush a logfile regularly (besides using buffered logger). I'm enclosing the patch to FileAppender. This introduces a new parameter called BufferFlushTime. This specifies how often (in seconds) should the logfile be flushed. If not specified, nothing happens. Use it freely if you wish. --- FileAppender.java.orig 2006-09-14 02:04:20.000000000 +0200 +++ FileAppender.java 2007-08-08 15:54:04.436673600 +0200 @@ -64,6 +64,37 @@ */ protected int bufferSize = 8*1024; + /** + * Sets the time in seconds to flush the logfile unconditionally. + */ + private int bufferFlushTime = -1; + + /** + * The instance that flushes the logfile periodically. + */ + private LogFlusher logFlusher = null; + + /** + * Inner class to periodically flush the logfile. + */ + class LogFlusher extends Thread { + private boolean stop = false; + + private void quit() { + stop = true; + } + + public void run() { + while (!stop) { + try { + Thread.sleep(FileAppender.this.bufferFlushTime * 1000); + } catch (InterruptedException ie) { + // ignore + } + FileAppender.this.flush(); + } + } + } /** The default constructor does not do anything. @@ -84,12 +115,17 @@ <p>If the <code>bufferedIO</code> parameter is <code>true</code>, then buffered IO will be used to write to the output file. + <p>If the <code>bufferFlushTime</code> parameter is set to a + non-negative value, then the file will be flushed every + <code>bufferFlushTime</code> seconds unconditionally. + */ public FileAppender(Layout layout, String filename, boolean append, boolean bufferedIO, - int bufferSize) throws IOException { + int bufferSize, final int bufferFlushTime) throws IOException { this.layout = layout; this.setFile(filename, append, bufferedIO, bufferSize); + setBufferFlushTime(bufferFlushTime); } /** @@ -214,6 +250,18 @@ /** + Get the value of the <b>BufferFlushTime</b> option. + + <p>Setting BufferFlushTime will cause the log file to be periodally flushed. + + */ + public + int getBufferFlushTime() { + return this.bufferFlushTime; + } + + + /** The <b>Append</b> option takes a boolean value. It is set to <code>true</code> by default. If true, then <code>File</code> will be opened in append mode by {@link #setFile setFile} (see @@ -256,6 +304,25 @@ } /** + Set the value of the <b>BufferFlushTime</b> option. + + <p>Setting BufferFlushTime will cause the log file to be periodally flushed. + + */ + public + void setBufferFlushTime(int bufferFlushTime) { + this.bufferFlushTime = bufferFlushTime; + + if (logFlusher != null) { + logFlusher.quit(); + } + logFlusher = new LogFlusher(); + logFlusher.setDaemon(true); + logFlusher.start(); + } + + + /** <p>Sets and <i>opens</i> the file where the log output will go. The specified file must be writable. @@ -339,5 +406,12 @@ this.fileName = null; super.reset(); } + + /** + Flush the logfile. */ + private + void flush() { + this.qw.flush(); + } }
*** This bug has been marked as a duplicate of 28647 ***
(In reply to comment #1) > > *** This bug has been marked as a duplicate of 28647 *** Hi, I don't think it is. 28647 is about flushing the file once a given severity message is logged. This patch is about flushing the file at regular time intervals. Ferenc
Closed out bug 28647 by providing an protected WriterAppender.shouldFlush(LoggingEvent) that could be used to implement any arbitrary flushing strategy. That hook should allow users to replace the flushing strategy with any arbitrary strategy\.