DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ· RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=43061>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ· INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43061 Summary: Flush appender regularty Product: Log4j Version: 1.2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P4 Component: Appender AssignedTo: [email protected] ReportedBy: [EMAIL PROTECTED] 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 [EMAIL PROTECTED] #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(); + } } -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
