We use the RollingFileAppender under Windows NT and simultaneously open the log file in a log viewer. Under these conditions, the appender no longer works because renaming a file that has been opened is not permitted, possibly resulting in a loss of data. There are not any problems under linux. We have found a way of solving this and use this derived appender. It is based on the idea that a rollover does not take place in case the file has been opened by a second application. This is tested through a renameTo using the original file name.We use the RollingFileAppender under Windows NT and simultaneously open the log file in a log viewer. Under these conditions, the appender no longer works because renaming a file that has been opened is not permitted, possibly resulting in a loss of data. There are not any problems under linux. We have found a way of solving this and use this derived appender. It is based on the idea that a rollover does not take place in case the file has been opened by a second application. This is tested through a renameTo using the original file name. Our derived appender class: package org.apache.log4j; import java.io.File; import java.io.IOException; public class RollingFileAppenderEx extends RollingFileAppender { public RollingFileAppenderEx() { super(); } public synchronized boolean canRename() { boolean bRet = false; File file; try { file = new File(fileName); this.closeFile(); bRet = file.renameTo(file); this.setFile(fileName, true); } catch(IOException e){} return bRet; } public synchronized void rollOver() { if (canRename() == true) { super.rollOver(); } } }
We have a related problem in that we have multiple output specifications all logging to the same file. We do this to have the layoutPattern distinct for different major subsystems. In this case, however, the RollingFileAppender does not "roll". IMO, ideal behavior would be for rolling behavior (for output specs to the same file) to be in place, with the caveat that if the various output specs specified different parameters for the rolling, which is selected is non- deterministic.
*** Bug 5211 has been marked as a duplicate of this bug. ***
*** This bug has been marked as a duplicate of 29726 ***