Bug 43061 - Flush appender regularty
Summary: Flush appender regularty
Status: RESOLVED FIXED
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Appender (show other bugs)
Version: 1.2
Hardware: All All
: P4 enhancement
Target Milestone: ---
Assignee: log4j-dev
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-08-08 07:12 UTC by Ferenc Toth
Modified: 2009-10-10 20:11 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ferenc Toth 2007-08-08 07:12:58 UTC
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();
+  }
 }
Comment 1 Curt Arnold 2007-08-22 20:25:32 UTC

*** This bug has been marked as a duplicate of 28647 ***
Comment 2 Ferenc Toth 2008-07-08 06:31:27 UTC
(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
Comment 3 Curt Arnold 2009-10-10 20:11:39 UTC
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\.