title: log4j:ERROR Failed to flush writer, java.io.IOException: The handle is invalid
body: <p>I am using log4j1.2.17 with my spring web app (Spring 3.2.1). I am getting the following issue intermittently. Sometimes it comes on startup itself and sometimes on request handling. Following is the stack trace.</p>
  
  <pre><code>log4j:ERROR Failed to flush writer,
  java.io.IOException: The handle is invalid
      at java.io.FileOutputStream.writeBytes(Native Method)
      at java.io.FileOutputStream.write(Unknown Source)
      at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
      at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
      at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
      at sun.nio.cs.StreamEncoder.flush(Unknown Source)
      at java.io.OutputStreamWriter.flush(Unknown Source)
      at org.apache.log4j.helpers.QuietWriter.flush(QuietWriter.java:59)
      at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:324)
      at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:276)
      at org.apache.log4j.WriterAppender.append(WriterAppender.java:162)
      at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
      at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
      at org.apache.log4j.Category.callAppenders(Category.java:206)
      at org.apache.log4j.Category.forcedLog(Category.java:391)
      at org.apache.log4j.Category.log(Category.java:856)
      at org.apache.commons.logging.impl.Log4JLogger.info(Log4JLogger.java:199)
  </code></pre>
  
  <p>When I debugged it appears that the file handle is closed already. Once when i was testing just before the exception, i got the following message from log4j</p>
  
  <pre><code>log4j:Finalizing appender named [stdout].
  log4j:Finalizing appender named [logfile].
  </code></pre>
  
  <p>Not sure why it is getting finalized when application is still using it.</p>
  
  <p>Below is my log4j xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
  &lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
  
  &lt;appender name="stdout" class="org.apache.log4j.ConsoleAppender"&gt;
      &lt;layout class="org.apache.log4j.PatternLayout"&gt;
          &lt;param name="ConversionPattern" value="%d %p [%c] -  %m%n" /&gt;
      &lt;/layout&gt;
  &lt;/appender&gt;
  
  &lt;appender name="logfile" class="org.apache.log4j.RollingFileAppender"&gt;
      &lt;param name="File"
          value="E:/logs/bizscore.log" /&gt;
      &lt;param name="MaxFileSize" value="5000KB" /&gt;
      &lt;param name="MaxBackupIndex" value="3" /&gt;
      &lt;layout class="org.apache.log4j.PatternLayout"&gt;
          &lt;param name="ConversionPattern" value="%d %p [%c] -  %m%n" /&gt;
      &lt;/layout&gt;
  &lt;/appender&gt;
  
  
  &lt;logger name="org.apache.commons"&gt;
      &lt;level value="INFO" /&gt;
  &lt;/logger&gt;
  
  
  
  &lt;logger name="org.hibernate"&gt;
      &lt;level value="INFO" /&gt;
  &lt;/logger&gt;
  
  &lt;logger name="org.springframework"&gt;
      &lt;level value="INFO" /&gt;
  &lt;/logger&gt;
  
  &lt;root&gt;
      &lt;priority value="info" /&gt;
      &lt;appender-ref ref="stdout" /&gt;
      &lt;appender-ref ref="logfile" /&gt;
  &lt;/root&gt;
  
  &lt;/log4j:configuration&gt;
  </code></pre>
  
  <p>Any pointer will be of great help.
  Thanks.</p>