carnold 2005/05/25 14:16:28 Modified: src/java/org/apache/log4j DailyRollingFileAppender.java RollingFileAppender.java src/java/org/apache/log4j/rolling FixedWindowRollingPolicy.java RollingFileAppender.java RollingPolicy.java RollingPolicyBase.java tests/src/java/org/apache/log4j/rolling ObsoleteRollingFileAppenderTest.java SizeBasedRollingTest.java TimeBasedRollingTest.java Added: tests/witness/rolling sbr-test4.log Removed: src/java/org/apache/log4j/rolling RolloverFailure.java src/java/org/apache/log4j/rolling/helper Compress.java Util.java Log: Bug 34979: Reworked RFA rename, eliminated need for FWRP.setActiveFileName Revision Changes Path 1.34 +3 -2 logging-log4j/src/java/org/apache/log4j/DailyRollingFileAppender.java Index: DailyRollingFileAppender.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/DailyRollingFileAppender.java,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- DailyRollingFileAppender.java 23 May 2005 00:21:56 -0000 1.33 +++ DailyRollingFileAppender.java 25 May 2005 21:16:27 -0000 1.34 @@ -29,8 +29,9 @@ * org.apache.log4j.DailyRollingFileAppender emulates earlier implementations * by delegating to general purpose org.apache.log4j.rollling.RollingFileAppender * introduced in log4j 1.3. This class is provided for compatibility with - * existing code and should not be used except when compatibility with version - * of log4j prior to 1.3 is a concern. + * existing configuration files but is not intended to be compatible with + * existing extensions of the prior RollingFileAppender and is marked final + * to prevent such use. * * @author Curt Arnold * @deprecated Replaced by [EMAIL PROTECTED] org.apache.log4j.rolling.RollingFileAppender} 1.27 +5 -4 logging-log4j/src/java/org/apache/log4j/RollingFileAppender.java Index: RollingFileAppender.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/RollingFileAppender.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- RollingFileAppender.java 23 May 2005 00:21:56 -0000 1.26 +++ RollingFileAppender.java 25 May 2005 21:16:27 -0000 1.27 @@ -30,9 +30,10 @@ /** * org.apache.log4j.RollingFileAppender emulates earlier implementations * by delegating to general purpose org.apache.log4j.rollling.RollingFileAppender - * introduced in log4j 1.3. This class is provided for compatibility with - * existing code and should not be used except when compatibility with version - * of log4j prior to 1.3 is a concern. + * introduced in log4j 1.3. This class is provided for compatibility with + * existing configuration files but is not intended to be compatible with + * existing extensions of the prior RollingFileAppender and is marked final + * to prevent such use. * * @author Curt Arnold * @deprecated Replaced by [EMAIL PROTECTED] org.apache.log4j.rolling.RollingFileAppender} @@ -164,7 +165,7 @@ FixedWindowRollingPolicy rolling = new FixedWindowRollingPolicy(); rolling.setMinIndex(1); rolling.setMaxIndex(maxBackupIndex); - rolling.setActiveFileName(rfa.getFile()); + rfa.setFile(rfa.getFile()); rolling.setFileNamePattern(rfa.getFile() + ".%i"); rolling.activateOptions(); rfa.setRollingPolicy(rolling); 1.12 +143 -93 logging-log4j/src/java/org/apache/log4j/rolling/FixedWindowRollingPolicy.java Index: FixedWindowRollingPolicy.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/FixedWindowRollingPolicy.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- FixedWindowRollingPolicy.java 25 May 2005 05:33:15 -0000 1.11 +++ FixedWindowRollingPolicy.java 25 May 2005 21:16:27 -0000 1.12 @@ -24,7 +24,9 @@ import org.apache.log4j.rolling.helper.ZipCompressAction; import java.io.File; -import java.io.IOException; + +import java.util.ArrayList; +import java.util.List; /** @@ -89,12 +91,18 @@ private int minIndex; /** + * if true, then an explicit name for the active file was + * specified using RollingFileAppender.file or the + * redundent RollingPolicyBase.setActiveFile + */ + private boolean explicitActiveFile; + + /** * Constructs a new instance. */ public FixedWindowRollingPolicy() { minIndex = 1; maxIndex = 7; - activeFileName = null; } /** @@ -109,13 +117,6 @@ throw new IllegalStateException(FNP_NOT_SET + SEE_FNP_NOT_SET); } - if (activeFileName == null) { - getLogger().warn( - "The ActiveFile name option must be set before using this rolling policy."); - throw new IllegalStateException( - "The ActiveFileName option must be set."); - } - if (maxIndex < minIndex) { getLogger().warn( "MaxIndex (" + maxIndex + ") cannot be smaller than MinIndex (" @@ -152,114 +153,68 @@ */ public RolloverDescription initialize( final String file, final boolean append) { + String newActiveFile = file; + explicitActiveFile = false; + if (activeFileName != null) { - return new RolloverDescriptionImpl(activeFileName, append, null, null); + explicitActiveFile = true; + newActiveFile = activeFileName; } - return null; + if (file != null) { + explicitActiveFile = true; + newActiveFile = file; + } + + if (!explicitActiveFile) { + StringBuffer buf = new StringBuffer(); + formatFileName(new Integer(minIndex), buf); + newActiveFile = buf.toString(); + } + + return new RolloverDescriptionImpl(newActiveFile, append, null, null); } /** * [EMAIL PROTECTED] */ - public RolloverDescription rollover(final String currentFileName) - throws IOException { + public RolloverDescription rollover(final String currentFileName) { if (maxIndex >= 0) { - // Delete the oldest file, to keep Windows happy. - StringBuffer buf = new StringBuffer(); - formatFileName(new Integer(maxIndex), buf); - - String higherFileName = buf.toString(); - File higherFile = new File(higherFileName); - - if (higherFile.exists()) { - if (!higherFile.delete()) { - throw new IOException("Unable to delete " + higherFileName); - } - } - - int suffixLength = 0; + int purgeStart = minIndex; - if (higherFileName.endsWith(".gz")) { - suffixLength = 3; - } else if (higherFileName.endsWith(".zip")) { - suffixLength = 4; + if (!explicitActiveFile) { + purgeStart++; } - String higherBaseName = higherFileName; - - if (suffixLength > 0) { - higherBaseName = - higherFileName.substring(0, higherFileName.length() - suffixLength); - - File baseFile = new File(higherBaseName); - - if (baseFile.exists()) { - if (!baseFile.delete()) { - throw new IOException("Unable to delete " + higherBaseName); - } - } - } - - // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1} - for (int i = maxIndex - 1; i >= minIndex; i--) { - buf.setLength(0); - formatFileName(new Integer(i), buf); - - String lowerFileName = buf.toString(); - File toRename = new File(lowerFileName); - - // no point in trying to rename an non-existent file - if (toRename.exists()) { - if (!toRename.renameTo(new File(higherFileName))) { - throw new IOException("Unable to rename " + lowerFileName); - } - } - - if (suffixLength > 0) { - String lowerBaseName = - lowerFileName.substring(0, lowerFileName.length() - suffixLength); - File baseFile = new File(lowerBaseName); - - if (baseFile.exists()) { - if (!baseFile.renameTo(new File(higherBaseName))) { - throw new IOException("Unable to rename " + lowerBaseName); - } - } - - higherBaseName = lowerBaseName; - } else { - higherBaseName = lowerFileName; - } - - higherFileName = lowerFileName; + if (!purge(purgeStart, maxIndex)) { + return null; } - File currentFile = new File(activeFileName); - - if (!currentFile.exists()) { - return new RolloverDescriptionImpl(activeFileName, false, null, null); - } + StringBuffer buf = new StringBuffer(); + formatFileName(new Integer(purgeStart), buf); - FileRenameAction renameAction = - new FileRenameAction( - new File(activeFileName), new File(higherBaseName), false); + String renameTo = buf.toString(); + String compressedName = renameTo; Action compressAction = null; - if (suffixLength == 3) { + if (renameTo.endsWith(".gz")) { + renameTo = renameTo.substring(0, renameTo.length() - 3); compressAction = new GZCompressAction( - new File(higherBaseName), new File(higherFileName), true, - getLogger()); - } else if (suffixLength == 4) { + new File(renameTo), new File(compressedName), true, getLogger()); + } else if (renameTo.endsWith(".zip")) { + renameTo = renameTo.substring(0, renameTo.length() - 4); compressAction = new ZipCompressAction( - new File(higherBaseName), new File(higherFileName), true, - getLogger()); + new File(renameTo), new File(compressedName), true, getLogger()); } + FileRenameAction renameAction = + new FileRenameAction( + new File(currentFileName), new File(renameTo), false); + return new RolloverDescriptionImpl( - activeFileName, false, renameAction, compressAction); + currentFileName, false, renameAction, compressAction); } return null; @@ -296,4 +251,99 @@ public void setMinIndex(int minIndex) { this.minIndex = minIndex; } + + /** + * Purge and rename old log files in preparation for rollover + * @param lowIndex low index + * @param highIndex high index. Log file associated with high + * index will be deleted if needed. + * @return true if purge was successful and rollover should be attempted. + */ + private boolean purge(final int lowIndex, final int highIndex) { + int suffixLength = 0; + + List renames = new ArrayList(); + StringBuffer buf = new StringBuffer(); + formatFileName(new Integer(lowIndex), buf); + + String lowFilename = buf.toString(); + + if (lowFilename.endsWith(".gz")) { + suffixLength = 3; + } else if (lowFilename.endsWith(".zip")) { + suffixLength = 4; + } + + for (int i = lowIndex; i <= highIndex; i++) { + File toRename = new File(lowFilename); + boolean isBase = false; + + if (suffixLength > 0) { + File toRenameBase = + new File( + lowFilename.substring(0, lowFilename.length() - suffixLength)); + + if (toRename.exists()) { + if (toRenameBase.exists()) { + toRenameBase.delete(); + } + } else { + toRename = toRenameBase; + isBase = true; + } + } + + if (toRename.exists()) { + // + // if at upper index then + // attempt to delete last file + // if that fails then abandon purge + if (i == highIndex) { + if (!toRename.delete()) { + return false; + } + + break; + } + + // + // if intermediate index + // add a rename action to the list + buf.setLength(0); + formatFileName(new Integer(i + 1), buf); + + String highFilename = buf.toString(); + String renameTo = highFilename; + + if (isBase) { + renameTo = + highFilename.substring(0, highFilename.length() - suffixLength); + } + + renames.add(new FileRenameAction(toRename, new File(renameTo), true)); + lowFilename = highFilename; + } else { + break; + } + } + + // + // work renames backwards + // + for (int i = renames.size() - 1; i >= 0; i--) { + Action action = (Action) renames.get(i); + + try { + if (!action.execute()) { + return false; + } + } catch (Exception ex) { + getLogger().info("Exception during purge in RollingFileAppender", ex); + + return false; + } + } + + return true; + } } 1.28 +19 -8 logging-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java Index: RollingFileAppender.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- RollingFileAppender.java 25 May 2005 05:33:15 -0000 1.27 +++ RollingFileAppender.java 25 May 2005 21:16:27 -0000 1.28 @@ -121,7 +121,7 @@ return; } - IOException ioException = null; + Exception exception = null; synchronized (this) { triggeringPolicy.activateOptions(); @@ -157,15 +157,15 @@ } super.activateOptions(); - } catch (IOException ex) { - ioException = ex; + } catch (Exception ex) { + exception = ex; } } - if (ioException != null) { + if (exception != null) { getLogger().warn( - "IOException while initializing RollingFileAppender named '" - + getName() + "'", ioException); + "Exception while initializing RollingFileAppender named '" + getName() + + "'", exception); } } @@ -288,8 +288,10 @@ writeHeader(); } + + return true; } - } catch (IOException ex) { + } catch (Exception ex) { exception = ex; } } @@ -312,7 +314,16 @@ if ( triggeringPolicy.isTriggeringEvent( this, event, getFile(), getFileLength())) { - rollover(); + // + // wrap rollover request in try block since + // rollover may fail in case read access to directory + // is not provided. However appender should still be in good + // condition and the append should still happen. + try { + rollover(); + } catch (Exception ex) { + getLogger().info("Exception during rollover attempt.", ex); + } } super.subAppend(event); 1.13 +4 -8 logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java Index: RollingPolicy.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- RollingPolicy.java 25 May 2005 05:33:15 -0000 1.12 +++ RollingPolicy.java 25 May 2005 21:16:27 -0000 1.13 @@ -18,8 +18,6 @@ import org.apache.log4j.spi.OptionHandler; -import java.io.IOException; - /** * A <code>RollingPolicy</code> specifies the actions taken @@ -37,10 +35,10 @@ * @param append current value of RollingFileAppender.getAppend(). * @return Description of the initialization, may be null to indicate * no initialization needed. - * @throws IOException on failure. + * @throws SecurityException if denied access to log files. */ public RolloverDescription initialize( - final String file, final boolean append) throws IOException; + final String file, final boolean append) throws SecurityException; /** * Prepare for a rollover. This method is called prior to @@ -51,9 +49,7 @@ * @param activeFile file name for current active log file. * @return Description of pending rollover, may be null to indicate no rollover * at this time. - * @throws IOException on failure to prepare for rollover. Rollover - * will be suppressed if an exception is thrown. + * @throws SecurityException if denied access to log files. */ - public RolloverDescription rollover(final String activeFile) - throws IOException; + public RolloverDescription rollover(final String activeFile) throws SecurityException; } 1.7 +4 -4 logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicyBase.java Index: RollingPolicyBase.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicyBase.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- RollingPolicyBase.java 25 May 2005 05:33:15 -0000 1.6 +++ RollingPolicyBase.java 25 May 2005 21:16:27 -0000 1.7 @@ -54,7 +54,7 @@ /** * Active file name may be null. - * @deprecated duplicates FileAppender.file and should be removed. + * Duplicates FileAppender.file and should be removed. */ protected String activeFileName; @@ -81,8 +81,8 @@ /** * ActiveFileName can be left unset, i.e. as null. - * @deprecated duplicates FileAppender.file and should be removed - * @see #getActiveFileName + * @param afn active file name. + * @deprecated Duplicates FileAppender.file and should be removed */ public void setActiveFileName(String afn) { activeFileName = afn; @@ -90,7 +90,7 @@ /** * Return the value of the <b>ActiveFile</b> option. - * @deprecated duplicates FileAppender.file and should be removed + * @deprecated Duplicates FileAppender.file and should be removed * @return active file name. */ public String getActiveFileName() { 1.3 +0 -4 logging-log4j/tests/src/java/org/apache/log4j/rolling/ObsoleteRollingFileAppenderTest.java Index: ObsoleteRollingFileAppenderTest.java =================================================================== RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/rolling/ObsoleteRollingFileAppenderTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ObsoleteRollingFileAppenderTest.java 8 Mar 2005 22:32:58 -0000 1.2 +++ ObsoleteRollingFileAppenderTest.java 25 May 2005 21:16:28 -0000 1.3 @@ -52,8 +52,6 @@ // Write exactly 10 bytes with each log for (int i = 0; i < 25; i++) { - Thread.sleep(100); - if (i < 10) { logger.debug("Hello---" + i); } else if (i < 100) { @@ -83,8 +81,6 @@ // Write exactly 10 bytes with each log for (int i = 0; i < 25; i++) { - Thread.sleep(100); - if (i < 10) { logger.debug("Hello---" + i); } else if (i < 100) { 1.15 +217 -25 logging-log4j/tests/src/java/org/apache/log4j/rolling/SizeBasedRollingTest.java Index: SizeBasedRollingTest.java =================================================================== RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/rolling/SizeBasedRollingTest.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- SizeBasedRollingTest.java 25 May 2005 05:33:15 -0000 1.14 +++ SizeBasedRollingTest.java 25 May 2005 21:16:28 -0000 1.15 @@ -26,6 +26,8 @@ import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.util.Compare; +import java.io.FileOutputStream; +import java.io.*; import java.io.File; @@ -56,32 +58,54 @@ } /** - * Test whether FixedWindowRollingPolicy throws an exception when - * the ActiveFileName is not set. + * Tests that the lack of an explicit active file will use the + * low index as the active file. + * */ - public void test1() throws Exception { - // We purposefully use the \n as the line separator. - // This makes the regression test system independent. - PatternLayout layout = new PatternLayout("%m\n"); - RollingFileAppender rfa = new RollingFileAppender(); - rfa.setAppend(false); - rfa.setLayout(layout); - - FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy(); - SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); - sbtp.setMaxFileSize(100); - sbtp.activateOptions(); - fwrp.setFileNamePattern("output/sizeBased-test1.%i"); - try { - fwrp.activateOptions(); - fail("The absence of activeFileName option should have caused an exception."); - } catch(IllegalStateException e) { - return; - } + public void test1() throws Exception { + PatternLayout layout = new PatternLayout("%m\n"); + RollingFileAppender rfa = new RollingFileAppender(); + rfa.setName("ROLLING"); + rfa.setAppend(false); + rfa.setLayout(layout); + + FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); + SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); + + sbtp.setMaxFileSize(100); + swrp.setMinIndex(0); + + swrp.setFileNamePattern("output/sizeBased-test1.%i"); + swrp.activateOptions(); + + rfa.setRollingPolicy(swrp); + rfa.setTriggeringPolicy(sbtp); + rfa.activateOptions(); + root.addAppender(rfa); + + // Write exactly 10 bytes with each log + for (int i = 0; i < 25; i++) { + if (i < 10) { + logger.debug("Hello---" + i); + } else if (i < 100) { + logger.debug("Hello--" + i); + } + } + + assertTrue(new File("output/sizeBased-test1.0").exists()); + assertTrue(new File("output/sizeBased-test1.1").exists()); + assertTrue(new File("output/sizeBased-test1.2").exists()); + + assertTrue(Compare.compare("output/sizeBased-test1.0", + "witness/rolling/sbr-test2.log")); + assertTrue(Compare.compare("output/sizeBased-test1.1", + "witness/rolling/sbr-test2.0")); + assertTrue(Compare.compare("output/sizeBased-test1.2", + "witness/rolling/sbr-test2.1")); } /** - * Test basic rolling functionality. + * Test basic rolling functionality with explicit setting of FileAppender.file. */ public void test2() throws Exception { PatternLayout layout = new PatternLayout("%m\n"); @@ -89,13 +113,13 @@ rfa.setName("ROLLING"); rfa.setAppend(false); rfa.setLayout(layout); + rfa.setFile("output/sizeBased-test2.log"); FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); sbtp.setMaxFileSize(100); swrp.setMinIndex(0); - swrp.setActiveFileName("output/sizeBased-test2.log"); swrp.setFileNamePattern("output/sizeBased-test2.%i"); swrp.activateOptions(); @@ -107,7 +131,6 @@ // Write exactly 10 bytes with each log for (int i = 0; i < 25; i++) { - Thread.sleep(100); if (i < 10) { logger.debug("Hello---" + i); } else if (i < 100) { @@ -141,7 +164,7 @@ sbtp.setMaxFileSize(100); fwrp.setMinIndex(0); - fwrp.setActiveFileName("output/sbr-test3.log"); + rfa.setFile("output/sbr-test3.log"); fwrp.setFileNamePattern("output/sbr-test3.%i.gz"); fwrp.activateOptions(); rfa.setRollingPolicy(fwrp); @@ -168,6 +191,175 @@ assertTrue(Compare.gzCompare("output/sbr-test3.1.gz", "witness/rolling/sbr-test3.1.gz")); } + /** + * Test basic rolling functionality with bogus path in file name pattern. + */ + public void test4() throws Exception { + PatternLayout layout = new PatternLayout("%m\n"); + RollingFileAppender rfa = new RollingFileAppender(); + rfa.setName("ROLLING"); + rfa.setAppend(false); + rfa.setLayout(layout); + rfa.setFile("output/sizeBased-test4.log"); + + FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); + SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); + + sbtp.setMaxFileSize(100); + swrp.setMinIndex(0); + + // + // test4 directory should not exists. Should cause all rollover attempts to fail. + // + swrp.setFileNamePattern("output/test4/sizeBased-test4.%i"); + swrp.activateOptions(); + + rfa.setRollingPolicy(swrp); + rfa.setTriggeringPolicy(sbtp); + rfa.activateOptions(); + root.addAppender(rfa); + + // Write exactly 10 bytes with each log + for (int i = 0; i < 25; i++) { + if (i < 10) { + logger.debug("Hello---" + i); + } else if (i < 100) { + logger.debug("Hello--" + i); + } + } + + assertTrue(new File("output/sizeBased-test4.log").exists()); + + assertTrue(Compare.compare("output/sizeBased-test4.log", + "witness/rolling/sbr-test4.log")); + } + + /** + * Checking handling of rename failures due to other access + * to the indexed files. + */ + public void test5() throws Exception { + PatternLayout layout = new PatternLayout("%m\n"); + RollingFileAppender rfa = new RollingFileAppender(); + rfa.setName("ROLLING"); + rfa.setAppend(false); + rfa.setLayout(layout); + rfa.setFile("output/sizeBased-test5.log"); + + FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); + SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); + + sbtp.setMaxFileSize(100); + swrp.setMinIndex(0); + + swrp.setFileNamePattern("output/sizeBased-test5.%i"); + swrp.activateOptions(); + + rfa.setRollingPolicy(swrp); + rfa.setTriggeringPolicy(sbtp); + rfa.activateOptions(); + root.addAppender(rfa); + + // + // put stray file about locked file + FileOutputStream os1 = new FileOutputStream("output/sizeBased-test5.1"); + os1.close(); + + + FileOutputStream os0 = new FileOutputStream("output/sizeBased-test5.0"); + + // Write exactly 10 bytes with each log + for (int i = 0; i < 25; i++) { + if (i < 10) { + logger.debug("Hello---" + i); + } else if (i < 100) { + logger.debug("Hello--" + i); + } + } + + os0.close(); + + if (new File("output/sizeBased-test5.3").exists()) { + // + // looks like platform where open files can be renamed + // + assertTrue(new File("output/sizeBased-test5.log").exists()); + assertTrue(new File("output/sizeBased-test5.0").exists()); + assertTrue(new File("output/sizeBased-test5.1").exists()); + assertTrue(new File("output/sizeBased-test5.2").exists()); + assertTrue(new File("output/sizeBased-test5.3").exists()); + + assertTrue(Compare.compare("output/sizeBased-test5.log", + "witness/rolling/sbr-test2.log")); + assertTrue(Compare.compare("output/sizeBased-test5.0", + "witness/rolling/sbr-test2.0")); + assertTrue(Compare.compare("output/sizeBased-test5.1", + "witness/rolling/sbr-test2.1")); + + } else { + // + // rollover attempts should all fail + // so initial log file should have all log content + // open file should be unaffected + // stray file should have only been moved one slot. + assertTrue(new File("output/sizeBased-test5.log").exists()); + assertTrue(new File("output/sizeBased-test5.0").exists()); + assertTrue(new File("output/sizeBased-test5.2").exists()); + + assertTrue(Compare.compare("output/sizeBased-test5.log", + "witness/rolling/sbr-test4.log")); + } + } + + + /** + * Test basic rolling functionality with explicit setting of + * obsolete FixedWindowRollingPolicy.activeFileName. + * @deprecated Tests deprecated method + */ + public void test6() throws Exception { + PatternLayout layout = new PatternLayout("%m\n"); + RollingFileAppender rfa = new RollingFileAppender(); + rfa.setName("ROLLING"); + rfa.setAppend(false); + rfa.setLayout(layout); + + FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); + SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy(); + + sbtp.setMaxFileSize(100); + swrp.setMinIndex(0); + swrp.setActiveFileName("output/sizeBased-test6.log"); + + swrp.setFileNamePattern("output/sizeBased-test6.%i"); + swrp.activateOptions(); + + rfa.setRollingPolicy(swrp); + rfa.setTriggeringPolicy(sbtp); + rfa.activateOptions(); + root.addAppender(rfa); + + // Write exactly 10 bytes with each log + for (int i = 0; i < 25; i++) { + if (i < 10) { + logger.debug("Hello---" + i); + } else if (i < 100) { + logger.debug("Hello--" + i); + } + } + + assertTrue(new File("output/sizeBased-test6.log").exists()); + assertTrue(new File("output/sizeBased-test6.0").exists()); + assertTrue(new File("output/sizeBased-test6.1").exists()); + + assertTrue(Compare.compare("output/sizeBased-test6.log", + "witness/rolling/sbr-test2.log")); + assertTrue(Compare.compare("output/sizeBased-test6.0", + "witness/rolling/sbr-test2.0")); + assertTrue(Compare.compare("output/sizeBased-test6.1", + "witness/rolling/sbr-test2.1")); + } + /** * Build test suite using this class and ObsoleteRollingFileAppenderTest. 1.20 +4 -4 logging-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTest.java Index: TimeBasedRollingTest.java =================================================================== RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTest.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- TimeBasedRollingTest.java 23 May 2005 23:51:01 -0000 1.19 +++ TimeBasedRollingTest.java 25 May 2005 21:16:28 -0000 1.20 @@ -239,7 +239,7 @@ rfa1.setLayout(layout1); TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy(); - tbrp1.setActiveFileName("output/test4.log"); + rfa1.setFile("output/test4.log"); tbrp1.setFileNamePattern("output/test4-%d{" + datePattern + "}"); tbrp1.activateOptions(); rfa1.setRollingPolicy(tbrp1); @@ -275,7 +275,7 @@ TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy(); tbrp2.setFileNamePattern("output/test4-%d{" + datePattern + "}"); - tbrp2.setActiveFileName("output/test4.log"); + rfa2.setFile("output/test4.log"); tbrp2.activateOptions(); rfa2.setRollingPolicy(tbrp2); rfa2.activateOptions(); @@ -305,7 +305,7 @@ TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); tbrp.setFileNamePattern("output/test5-%d{" + datePattern + "}"); - tbrp.setActiveFileName("output/test5.log"); + rfa.setFile("output/test5.log"); tbrp.activateOptions(); rfa.setRollingPolicy(tbrp); rfa.activateOptions(); @@ -349,7 +349,7 @@ TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); tbrp.setFileNamePattern("output/test6-%d{" + datePattern + "}.gz"); - tbrp.setActiveFileName("output/test6.log"); + rfa.setFile("output/test6.log"); tbrp.activateOptions(); rfa.setRollingPolicy(tbrp); rfa.activateOptions(); 1.1 logging-log4j/tests/witness/rolling/sbr-test4.log Index: sbr-test4.log =================================================================== Hello---0 Hello---1 Hello---2 Hello---3 Hello---4 Hello---5 Hello---6 Hello---7 Hello---8 Hello---9 Hello--10 Hello--11 Hello--12 Hello--13 Hello--14 Hello--15 Hello--16 Hello--17 Hello--18 Hello--19 Hello--20 Hello--21 Hello--22 Hello--23 Hello--24
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]