You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2004/11/23 17:30:14 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/rolling/helpers Compress.java RollingCalendar.java

ceki        2004/11/23 08:30:14

  Modified:    tests/src/java/org/apache/log4j/rolling
                        TimeBasedRollingTest.java SizeBasedRollingTest.java
               src/java/org/apache/log4j/rolling RollingPolicy.java
                        RollingFileAppender.java
                        SlidingWindowRollingPolicy.java
                        RollingPolicySkeleton.java TriggeringPolicy.java
                        TimeBasedRollingPolicy.java
               src/java/org/apache/log4j/rolling/helpers Compress.java
                        RollingCalendar.java
  Added:       src/java/org/apache/log4j/rolling package.html
  Log:
  - Refactoring and simplifications of various rolling policies
  - Slightly better docs
  
  Revision  Changes    Path
  1.11      +6 -5      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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TimeBasedRollingTest.java	22 Nov 2004 20:21:01 -0000	1.10
  +++ TimeBasedRollingTest.java	23 Nov 2004 16:30:13 -0000	1.11
  @@ -20,6 +20,7 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.log4j.ConsoleAppender;
   import org.apache.log4j.LogManager;
   import org.apache.log4j.Logger;
   import org.apache.log4j.PatternLayout;
  @@ -45,9 +46,9 @@
     }
   
     public void setUp() {
  -    //root = Logger.getRootLogger();
  -    //root.addAppender(
  -      //new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} [%t] %level %c - %m%n")));
  +    Logger root = Logger.getRootLogger();
  +    root.addAppender(
  +      new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} [%t] %level %c{2}#%M:%L - %m%n")));
     }
   
     public void tearDown() {
  @@ -404,12 +405,12 @@
   
   //    suite.addTest(new TimeBasedRollingTest("test1"));
   //    suite.addTest(new TimeBasedRollingTest("test2"));
  -//    suite.addTest(new TimeBasedRollingTest("test3"));
  +    suite.addTest(new TimeBasedRollingTest("test3"));
   //    suite.addTest(new TimeBasedRollingTest("test4"));
   //
   //    suite.addTest(new TimeBasedRollingTest("test5"));
   //    suite.addTest(new TimeBasedRollingTest("test6"));
  -    suite.addTest(new TimeBasedRollingTest("testWithJoran1"));
  +//    suite.addTest(new TimeBasedRollingTest("testWithJoran1"));
       
       
       return suite;
  
  
  
  1.4       +2 -2      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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SizeBasedRollingTest.java	31 May 2004 21:14:24 -0000	1.3
  +++ SizeBasedRollingTest.java	23 Nov 2004 16:30:13 -0000	1.4
  @@ -129,10 +129,10 @@
        SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
        SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
   
  -     swrp.setCompressionMode("GZ");
  +     //swrp.setCompressionMode("GZ");
        sbtp.setMaxFileSize(100);
        swrp.setActiveFileName("output/sizeBased-test3");
  -     swrp.setFileNamePattern("output/sizeBased-test3.%i");
  +     swrp.setFileNamePattern("output/sizeBased-test3.%i.gz");
        swrp.activateOptions();
        rfa.setRollingPolicy(swrp);
        rfa.setTriggeringPolicy(sbtp);
  
  
  
  1.9       +14 -10    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- RollingPolicy.java	22 Nov 2004 20:20:58 -0000	1.8
  +++ RollingPolicy.java	23 Nov 2004 16:30:14 -0000	1.9
  @@ -20,25 +20,29 @@
   import org.apache.log4j.spi.OptionHandler;
   
   /**
  - * A <code>RollingPolicy</code> is responsible for copying a log  file after 
  - * the occurence of a rollover triggering event. The RollingPolicy is also 
  - * responsible for providing the log file where logging output will be 
  - * directed. This file is referred to as the <em>active log file</em>.
  - *
  + * A <code>RollingPolicy</code> is responsible for performing the
  + * rolling over of the active log file. The <code>RollingPolicy</code>
  + * is also responsible for providing the <em>active log file</em>,
  + * that is the live file where logging output will be directed.
  + * 
    * @author Ceki G&uuml;lc&uuml;
    * @since 1.3
    * */
   public interface RollingPolicy extends OptionHandler {
  -    
  +  
     /**
  +   * Rolls over log files according to implementation policy.  
  +   * <p>
  +   * <p>This method is invoked by {@link RollingFileAppender}, usually 
  +   * at the behest of its {@link TriggeringPolicy}.
      * 
  -   * Rollover log files according to implementation policy.
  -   * .
  -   * */
  +   * @throws RolloverFailure Thrown if the rollover operation fails for any
  +   * reason.
  +   */
     public void rollover() throws RolloverFailure;
   
     /**
      * Get the new name of the active log file.
      * */  
  -  public String getActiveLogFileName();
  +  public String getActiveFileName();
   }
  
  
  
  1.20      +11 -14    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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- RollingFileAppender.java	22 Nov 2004 20:20:58 -0000	1.19
  +++ RollingFileAppender.java	23 Nov 2004 16:30:14 -0000	1.20
  @@ -17,8 +17,6 @@
   package org.apache.log4j.rolling;
   
   import org.apache.log4j.FileAppender;
  -import org.apache.log4j.Logger;
  -import org.apache.log4j.spi.ErrorCode;
   import org.apache.log4j.spi.LoggingEvent;
   
   import java.io.File;
  @@ -36,12 +34,13 @@
    * set up. For example, {@link TimeBasedRollingPolicy} acts both as a
    * <code>RollingPolicy</code> and a <code>TriggeringPolicy</code>.
    * 
  + * 
  + * 
    * @author Heinz Richter
    * @author Ceki G&uuml;lc&uuml;
    * @since  1.3
    * */
   public class RollingFileAppender extends FileAppender {
  -  Logger logger = Logger.getLogger(RollingFileAppender.class);
     File activeFile;
     TriggeringPolicy triggeringPolicy;
     RollingPolicy rollingPolicy;
  @@ -56,22 +55,21 @@
   
     public void activateOptions() {
       if (triggeringPolicy == null) {
  -      logger.warn("Please set a TriggeringPolicy for ");
  -
  +      getLogger().warn("Please set a TriggeringPolicy for the RollingFileAppender named '{}'", getName());
         return;
       }
   
       if (rollingPolicy != null) {
  -      String afn = rollingPolicy.getActiveLogFileName();
  +      String afn = rollingPolicy.getActiveFileName();
         activeFile = new File(afn);
  -      logger.debug("Active log file name: "+afn);
  +      getLogger().debug("Active log file name: "+afn);
         setFile(afn);
         
         // the activeFile variable is used by the triggeringPolicy.isTriggeringEvent method
         activeFile = new File(afn);
         super.activateOptions();
       } else {
  -      logger.warn("Please set a rolling policy");
  +      getLogger().warn("Please set a rolling policy");
       }
     }
   
  @@ -110,8 +108,8 @@
       }
       
       // Although not certain, the active file name may change after roll over.
  -    fileName = rollingPolicy.getActiveLogFileName();
  -    logger.debug("Active file name is now ["+fileName+"].");
  +    fileName = rollingPolicy.getActiveFileName();
  +    getLogger().debug("Active file name is now [{}].", fileName);
   
       // the activeFile variable is used by the triggeringPolicy.isTriggeringEvent method
       activeFile = new File(fileName);
  @@ -121,9 +119,8 @@
         // close operations are safe.
         this.setFile(fileName, append, bufferedIO, bufferSize);
       } catch (IOException e) {
  -      errorHandler.error(
  -        "setFile(" + fileName + ", false) call failed.", e,
  -        ErrorCode.FILE_OPEN_FAILURE);
  +      getLogger().error(
  +        "setFile(" + fileName + ", false) call failed.", e);
       }
     }
   
  @@ -135,7 +132,7 @@
       // The rollover check must precede actual writing. This is the 
       // only correct behavior for time driven triggers. 
       if (triggeringPolicy.isTriggeringEvent(activeFile)) {
  -      logger.debug("About to rollover");
  +      getLogger().debug("About to rollover");
         rollover();
       }
         
  
  
  
  1.13      +49 -64    logging-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java
  
  Index: SlidingWindowRollingPolicy.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- SlidingWindowRollingPolicy.java	31 May 2004 21:29:15 -0000	1.12
  +++ SlidingWindowRollingPolicy.java	23 Nov 2004 16:30:14 -0000	1.13
  @@ -1,12 +1,12 @@
   /*
    * Copyright 1999,2004 The Apache Software Foundation.
  - * 
  + *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
  - * 
  + *
    *      http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  @@ -21,7 +21,6 @@
   import org.apache.log4j.rolling.helpers.FileNamePattern;
   import org.apache.log4j.rolling.helpers.Util;
   
  -
   import java.io.File;
   
   
  @@ -32,20 +31,45 @@
    * @since 1.3
    * */
   public class SlidingWindowRollingPolicy extends RollingPolicySkeleton {
  -    
     static Logger logger = Logger.getLogger(SlidingWindowRollingPolicy.class);
     int maxIndex;
     int minIndex;
     FileNamePattern fileNamePattern;
  -  String  fileNamePatternStr;
  +  String fileNamePatternStr;
     String activeFileName;
  - 
  +
     public SlidingWindowRollingPolicy() {
       minIndex = 1;
       maxIndex = 7;
       activeFileName = null;
     }
   
  +  public void activateOptions() {
  +    if (activeFileName == null) {
  +      logger.warn(
  +        "The active file name option must be set before using this rolling policy.");
  +      throw new IllegalStateException(
  +        "The activeFileName option must be set.");
  +    }
  +
  +    if (maxIndex < minIndex) {
  +      logger.warn(
  +        "maxIndex (" + maxIndex + ") cannot be smaller than minIndex ("
  +        + minIndex + ").");
  +      logger.warn("Setting maxIndex to equal minIndex.");
  +      maxIndex = minIndex;
  +    }
  +
  +    switch (compressionMode) {
  +    case Compress.GZ:
  +      if (!fileNamePatternStr.endsWith(".gz")) {
  +        fileNamePatternStr = fileNamePatternStr + ".gz";
  +      }
  +      break;
  +    }
  +    fileNamePattern = new FileNamePattern(fileNamePatternStr);
  +  }
  +
     public void rollover() throws RolloverFailure {
       // Inside this method it is guaranteed that the hereto active log fil is closed.
       // If maxIndex <= 0, then there is no file renaming to be done.
  @@ -65,59 +89,33 @@
   
         if (activeFileName != null) {
           //move active file name to min
  -        switch(compressionMode) {
  -          case Compress.NONE: 
  -            Util.rename(activeFileName, fileNamePattern.convert(minIndex));
  -            break;
  -            case Compress.GZ: 
  -              Compress.GZCompress(activeFileName, fileNamePattern.convert(minIndex));
  -              break;
  +        switch (compressionMode) {
  +        case Compress.NONE:
  +          Util.rename(activeFileName, fileNamePattern.convert(minIndex));
  +          break;
  +        case Compress.GZ:
  +          Compress.GZCompress(
  +            activeFileName, fileNamePattern.convert(minIndex));
  +          break;
           }
         }
       }
     }
   
  -  public void activateOptions() {
  -    if(activeFileName == null) {
  -      logger.warn("The active file name option must be set before using this rolling policy.");
  -      throw new IllegalStateException("The activeFileName option must be set.");
  -    }
  -    
  -    if (maxIndex < minIndex) {
  -      logger.warn(
  -        "maxIndex (" + maxIndex + ") cannot be smaller than minIndex ("
  -        + minIndex + ").");
  -      logger.warn("Setting maxIndex to equal minIndex.");
  -      maxIndex = minIndex;
  -    }
  -    
  -    switch(compressionMode) {
  -      case Compress.GZ: 
  -      if(!fileNamePatternStr.endsWith(".gz")) {    
  -        fileNamePatternStr = fileNamePatternStr + ".gz";
  -      } 
  -      break;
  -    }      
  -    fileNamePattern = new FileNamePattern(fileNamePatternStr);
  -  }
  -
     /**
  -   *
  -   * If the <b>ActiveFileName</b> option is set, then this method simply returns the
  -   * value of the option. Otherwise, it returns the value of <b>FileNamePattern</b>
  -   * for <b>MaxIndex</b>. For example, if <b>ActiveFileName</b> is not set and
  -   * <b>FileNamePattern</b> is set to "mylogfile.%i" and <b>MaxIndex</b> is set to 0,
  -   * then this method will return "mylogfile.0".
  -   *
  -   */
  -  public String getActiveLogFileName() {
  +  *
  +  * If the <b>ActiveFileName</b> option is set, then this method simply returns the
  +  * value of the option. Otherwise, it returns the value of <b>FileNamePattern</b>
  +  * for <b>MaxIndex</b>. For example, if <b>ActiveFileName</b> is not set and
  +  * <b>FileNamePattern</b> is set to "mylogfile.%i" and <b>MaxIndex</b> is set to 0,
  +  * then this method will return "mylogfile.0".
  +  *
  +  */
  +  public String getActiveFileName() {
  +    // TODO This is clearly bogus.
       return activeFileName;
     }
   
  -  public String getFileNamePattern() {
  -    return fileNamePatternStr;
  -  }
  -
     public int getMaxIndex() {
       return maxIndex;
     }
  @@ -126,25 +124,12 @@
       return minIndex;
     }
   
  -  public void setFileNamePattern(String fnp) {
  -    fileNamePatternStr = fnp;
  -  }
  -
     public void setMaxIndex(int maxIndex) {
       this.maxIndex = maxIndex;
     }
   
     public void setMinIndex(int minIndex) {
       this.minIndex = minIndex;
  -  }
  -
  -
  -  public String getActiveFileName() {
  -    return activeFileName;
  -  }
  -
  -  public void setActiveFileName(String afn) {
  -    activeFileName = afn;
     }
   
   }
  
  
  
  1.5       +71 -30    logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicySkeleton.java
  
  Index: RollingPolicySkeleton.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/RollingPolicySkeleton.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RollingPolicySkeleton.java	31 May 2004 21:29:15 -0000	1.4
  +++ RollingPolicySkeleton.java	23 Nov 2004 16:30:14 -0000	1.5
  @@ -1,58 +1,99 @@
   /*
    * Copyright 1999,2004 The Apache Software Foundation.
  - * 
  + *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
  - * 
  + *
    *      http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
  +
   package org.apache.log4j.rolling;
   
  +import org.apache.log4j.LogManager;
  +import org.apache.log4j.Logger;
   import org.apache.log4j.rolling.helpers.Compress;
  +import org.apache.log4j.rolling.helpers.FileNamePattern;
  +
   
   /**
  - * 
  + * Implements methods common to most, it not all, rolling
  + * policies. Currently such methods are limited to a compression mode
  + * getter/setter.
  + *
    * @author Ceki G&uuml;lc&uuml;
  + * @since 1.3
    */
  -abstract public class RollingPolicySkeleton implements RollingPolicy {
  -  int compressionMode = Compress.NONE;
  -  
  -  
  -  /* (non-Javadoc)
  +public abstract class RollingPolicySkeleton implements RollingPolicy {
  +  protected int compressionMode = Compress.NONE;
  +  protected FileNamePattern fileNamePattern;
  +  protected String fileNamePatternStr;
  +  protected String activeFileName;
  +  private Logger logger;
  +
  +  /*
      * @see org.apache.log4j.spi.OptionHandler#activateOptions()
      */
  -  abstract public void activateOptions();
  -  
  -  public String getCompressionMode() {
  -    switch(compressionMode) {
  -      case Compress.NONE: return Compress.NONE_STR;
  -      case Compress.GZ: return Compress.GZ_STR;
  -      case Compress.ZIP: return Compress.ZIP_STR;
  -    }
  -    return Compress.NONE_STR;
  -  }
  +  public abstract void activateOptions();
   
  +  /**
  +   * Given the FileNamePattern string, this method determines the compression
  +   * mode depending on last letters of the fileNamePatternStr. Patterns
  +   * ending with .gz imply GZIP compression, endings with '.zip' imply
  +   * ZIP compression. Otherwise and by default, there is no compression.
  +   *
  +   */
  +  protected void determineCompressionMode() {
  +    int len = fileNamePatternStr.length();
   
  -  public void setCompressionMode(String compMode) {
  -    System.out.println("================"+compMode);
  -    if(compMode == null) {
  -      compressionMode = Compress.NONE;
  -    }
  -    compMode = compMode.trim();
  -    if(compMode.equalsIgnoreCase(Compress.GZ_STR)) {
  -      System.out.println("==================================");
  +    if (fileNamePatternStr.endsWith(".gz")) {
  +      getLogger().debug("Will use gz compression");
  +      fileNamePattern =
  +        new FileNamePattern(fileNamePatternStr.substring(0, len - 3));
  +      compressionMode = Compress.GZ;
  +    } else if (fileNamePatternStr.endsWith(".zip")) {
  +      getLogger().debug("Will use zip compression");
  +      fileNamePattern =
  +        new FileNamePattern(fileNamePatternStr.substring(0, len - 4));
         compressionMode = Compress.GZ;
  -    } else if (compMode.equalsIgnoreCase(Compress.ZIP_STR)) {
  -      compressionMode = Compress.ZIP;
       } else {
  +      getLogger().debug("No compression will be used");
  +      fileNamePattern = new FileNamePattern(fileNamePatternStr);
         compressionMode = Compress.NONE;
  -    }   
  +    }
  +  }
  +
  +  public void setFileNamePattern(String fnp) {
  +    fileNamePatternStr = fnp;
  +  }
  +
  +  public String getFileNamePattern() {
  +    return fileNamePatternStr;
  +  }
  +
  +  /**
  +   * ActiveFileName can be left unset, i.e. as null.
  +   * @see #getActiveFileName
  +   */
  +  public void setActiveFileName(String afn) {
  +    activeFileName = afn;
  +  }
  +
  +  /**
  +   * Return an instance specific logger to be used by the policy itself.
  +   *
  +   * @return instance specific logger
  +   */
  +  protected Logger getLogger() {
  +    if (logger == null) {
  +      logger = LogManager.getLogger(this.getClass().getName());
  +    }
  +    return logger;
     }
   }
  
  
  
  1.5       +4 -4      logging-log4j/src/java/org/apache/log4j/rolling/TriggeringPolicy.java
  
  Index: TriggeringPolicy.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/TriggeringPolicy.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TriggeringPolicy.java	22 Nov 2004 20:20:58 -0000	1.4
  +++ TriggeringPolicy.java	23 Nov 2004 16:30:14 -0000	1.5
  @@ -21,19 +21,19 @@
   import org.apache.log4j.spi.OptionHandler;
   /**
    * A <code>TriggeringPolicy</code> controls the conditions under which rollover
  - * occurs. However, triggering policy might depend on time, file size, an 
  - * external condition or a combination thereof.
  + * occurs. Such conditions include time od day, file size, an 
  + * external event or a combination thereof.
    *
    * @author Ceki G&uuml;lc&uuml;
    * @since 1.3
    * */
   
   public interface TriggeringPolicy extends OptionHandler {
  +  
     /**
      * Should rolllover be triggered at this time?
      * 
  -   * A reference to the active log file is supplied as a parameter.
  -   * 
  +   * @param file A reference to the currently active log file. 
      * */
     public boolean isTriggeringEvent(File file);
   }
  
  
  
  1.9       +87 -125   logging-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java
  
  Index: TimeBasedRollingPolicy.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TimeBasedRollingPolicy.java	22 Nov 2004 17:07:08 -0000	1.8
  +++ TimeBasedRollingPolicy.java	23 Nov 2004 16:30:14 -0000	1.9
  @@ -1,12 +1,12 @@
   /*
    * Copyright 1999,2004 The Apache Software Foundation.
  - * 
  + *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
  - * 
  + *
    *      http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  @@ -16,10 +16,8 @@
   
   package org.apache.log4j.rolling;
   
  -import org.apache.log4j.Logger;
   import org.apache.log4j.rolling.helpers.Compress;
   import org.apache.log4j.rolling.helpers.DateTokenConverter;
  -import org.apache.log4j.rolling.helpers.FileNamePattern;
   import org.apache.log4j.rolling.helpers.RollingCalendar;
   import org.apache.log4j.rolling.helpers.Util;
   
  @@ -30,173 +28,137 @@
   
   /**
    *
  - * 
  + *
    * If configuring programatically, do not forget to call {@link #activateOptions}
  - * method before using this policy.
  - * 
  + * method before using this policy. Moreover, {@link #activateOptions} of
  + * <code> TimeBasedRollingPolicy</code> must be called <em>before</em> calling
  + * the {@link #activateOptions} method of the owning
  + * <code>RollingFileAppender</code>.
  + *
    * @author Ceki G&uuml;lc&uuml;
    */
   public class TimeBasedRollingPolicy extends RollingPolicySkeleton
     implements TriggeringPolicy {
  -    
  -  static final Logger logger = Logger.getLogger(TimeBasedRollingPolicy.class);
  -  
  -  FileNamePattern fileNamePattern;
  -  String fileNamePatternStr;
  +  static final String FNP_NOT_SET =
  +    "The FileNamePattern option must be set before using TimeBasedRollingPolicy. ";
  +  static final String SEE_FNP_NOT_SET =
  +    "See also http://logging.apache.org/log4j/codes.html#tbr_fnp_not_set";
     RollingCalendar rc;
     long nextCheck;
  -  Date now = new Date();
  -  String currentFileName = null;
  -  String activeFileName;
  +  Date lastCheck = new Date();
  +  String elapsedPeriodsFileName;
   
     public void activateOptions() {
       // find out period from the filename pattern
       if (fileNamePatternStr != null) {
  -      int len = fileNamePatternStr.length();
  -      
  -      if (fileNamePatternStr.endsWith(".gz")) {
  -          logger.debug("Will use gz compression");
  -          fileNamePattern = new FileNamePattern(fileNamePatternStr.substring(0, len -3));
  -          compressionMode = Compress.GZ;
  -        } else if (fileNamePatternStr.endsWith(".zip")) {
  -          logger.debug("Will use zip compression");
  -          fileNamePattern = new FileNamePattern(fileNamePatternStr.substring(0, len -4));
  -          compressionMode = Compress.GZ;
  -        } else {
  -          logger.debug("No compression will be used");
  -          fileNamePattern = new FileNamePattern(fileNamePatternStr);
  -          compressionMode = Compress.NONE;
  -        }
  -      }
  -      
  -      DateTokenConverter dtc = fileNamePattern.getDateTokenConverter();
  -
  -      if (dtc == null) {
  -        throw new IllegalStateException(
  -          "FileNamePattern [" + fileNamePattern.getPattern()
  -          + "] does not contain a valid DateToken");
  -      }
  -
  -      rc = new RollingCalendar();
  -      rc.init(dtc.getDatePattern());
  -      logger.debug(
  -        "The date pattern is [" + dtc.getDatePattern()
  -        + "] from file name pattern [" + fileNamePattern.getPattern() + "].");
  -      rc.printPeriodicity();
  -
  -      long n = System.currentTimeMillis();
  -      now.setTime(n);
  -      nextCheck = rc.getNextCheckMillis(now);
  -
  -      //Date nc = new Date();
  -      //nc.setTime(nextCheck);
  -      //logger.debug("Next check set to: " + nc);  
  -  }
  +      determineCompressionMode();
  +    } else {
  +      getLogger().warn(FNP_NOT_SET);
  +      getLogger().warn(SEE_FNP_NOT_SET);
  +      throw new IllegalStateException(FNP_NOT_SET + SEE_FNP_NOT_SET);
  +    }
   
  -  public void rollover() throws RolloverFailure {
  -    logger.debug("rollover called");
  -    logger.debug("compressionMode: " + compressionMode);
  +    DateTokenConverter dtc = fileNamePattern.getDateTokenConverter();
   
  +    if (dtc == null) {
  +      throw new IllegalStateException(
  +        "FileNamePattern [" + fileNamePattern.getPattern()
  +        + "] does not contain a valid DateToken");
  +    }
   
  -    // if active file name is not set, then the active logging 
  -    // file is given by the value of currentFileName variable.
  -    if (activeFileName == null) {
  -      if (currentFileName != null) {
  -        //logger.debug("currentFileName != null");
  +    rc = new RollingCalendar();
  +    rc.init(dtc.getDatePattern());
  +    getLogger().debug(
  +      "The date pattern is '{}' from file name pattern '{}'.",
  +      dtc.getDatePattern(), fileNamePattern.getPattern());
  +    rc.printPeriodicity();
   
  -        switch (compressionMode) {
  -        case Compress.NONE:
  +    long n = System.currentTimeMillis();
  +    lastCheck.setTime(n);
  +    nextCheck = rc.getNextCheckMillis(lastCheck);
   
  -          // nothing to do;
  -          break;
  +    //Date nc = new Date();
  +    //nc.setTime(nextCheck);
  +    //getLogger().debug("Next check set to: " + nc);  
  +  }
   
  -        case Compress.GZ:
  -          logger.debug("Compressing [" + currentFileName + "]");
  -          Compress.GZCompress(currentFileName);
  +  public void rollover() throws RolloverFailure {
  +    getLogger().debug("rollover called");
  +    getLogger().debug("compressionMode: " + compressionMode);
   
  -          break;
  -        }
  +    if (activeFileName == null) {
  +      switch (compressionMode) {
  +      case Compress.NONE:
  +        // nothing to do;
  +        break;
  +      case Compress.GZ:
  +        getLogger().debug("GZIP compressing [{}]", elapsedPeriodsFileName);
  +        Compress.GZCompress(elapsedPeriodsFileName);
  +        break;
  +      case Compress.ZIP:
  +        getLogger().debug("ZIP compressing [{}]", elapsedPeriodsFileName);
  +        Compress.ZIPCompress(elapsedPeriodsFileName);
  +        break;
         }
  -    } else { // if activeFileName != null, then the value of the
  -      // active logging is given by activeFileName
  -
  +    } else {
         switch (compressionMode) {
         case Compress.NONE:
  -        Util.rename(activeFileName, currentFileName);
  -
  +        Util.rename(activeFileName, elapsedPeriodsFileName);
           break;
  -
         case Compress.GZ:
  -        logger.debug("Compressing [" + currentFileName + "]");
  -        Compress.GZCompress(activeFileName, currentFileName);
  -
  +        getLogger().debug("GZIP compressing [[}]", elapsedPeriodsFileName);
  +        Compress.GZCompress(activeFileName, elapsedPeriodsFileName);
  +        break;
  +      case Compress.ZIP:
  +        getLogger().debug("ZIP compressing [[}]", elapsedPeriodsFileName);
  +        Compress.ZIPCompress(activeFileName, elapsedPeriodsFileName);
           break;
         }
       }
     }
   
     /**
  -   * 
  -   * The active log file is determined by the value of the activeFileName 
  -   * option if it is set. However, in case the activeFileName is left blank, 
  -   * then, the active log file equals the file name for the current period
  -   * as computed by the fileNamePattern option.
  -   * 
  -   */
  -  public String getActiveLogFileName() {
  -    logger.debug("getActiveLogFileName called");
  +  *
  +  * The active log file is determined by the value of the activeFileName
  +  * option if it is set. However, in case the activeFileName is left blank,
  +  * then, the active log file equals the file name for the current period
  +  * as computed by the <b>FileNamePattern</b> option.
  +  *
  +  */
  +  public String getActiveFileName() {
  +    getLogger().debug("getActiveLogFileName called");
       if (activeFileName == null) {
  -      return fileNamePattern.convert(now);
  +      return fileNamePattern.convert(lastCheck);
       } else {
         return activeFileName;
       }
     }
   
  -  public void setFileNamePattern(String fnp) {
  -    fileNamePatternStr = fnp;
  -  }
  -
     public boolean isTriggeringEvent(File file) {
  -    //logger.debug("Is triggering event called");
  +    //getLogger().debug("Is triggering event called");
       long n = System.currentTimeMillis();
   
       if (n >= nextCheck) {
  -      logger.debug("Time to trigger rollover");
  +      getLogger().debug("Time to trigger rollover");
   
  -      // We set the oldFileName before we set the 'now' variable
  -      // The currentFileName is the currently active file name when
  -      // the activeFileName is not set specifically by the user.
  -      currentFileName = fileNamePattern.convert(now);
  -
  -      now.setTime(n);
  -      //logger.debug("ActiveLogFileName will return " + getActiveLogFileName());
  -      nextCheck = rc.getNextCheckMillis(now);
  +      // We set the elapsedPeriodsFileName before we set the 'lastCheck' variable
  +      // The elapsedPeriodsFileName corresponds to the file name of the period
  +      // that just elapsed.
  +      elapsedPeriodsFileName = fileNamePattern.convert(lastCheck);
  +      getLogger().debug(
  +        "elapsedPeriodsFileName set to {}", elapsedPeriodsFileName);
  +
  +      lastCheck.setTime(n);
  +      //getLogger().debug("ActiveLogFileName will return " + getActiveLogFileName());
  +      nextCheck = rc.getNextCheckMillis(lastCheck);
   
  -      //logger.debug("nextCheck is :"+nextCheck);
         Date x = new Date();
         x.setTime(nextCheck);
  -      logger.debug("Next check: " + x);
  +      getLogger().debug("Next check on {}", x);
   
         return true;
       } else {
         return false;
       }
  -  }
  -
  -  /**
  -   * ActiveFileName can be null.
  -   * 
  -   * @see #getActiveLogFileName
  -   * */
  -  public String getActiveFileName() {
  -    return activeFileName;
  -  }
  -
  -  /**
  -   * ActiveFileName can be left unset, i.e. as null.
  -   * @see #getActiveLogFileName
  -   */
  -  public void setActiveFileName(String string) {
  -    activeFileName = string;
     }
   }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/rolling/package.html
  
  Index: package.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  
  <html> 
    <head>
      <title></title>
    </head>
    
    <body>
      
      <p>Implements various file rolling policies.</p>
  
      <p>The {@link org.apache.log4j.rolling.RollingFileAppender} class
      serves as the linchpin of this package. Its behaviour is
      controlled by two subcomponents of type {@link
      org.apache.log4j.rolling.RollingPolicy} and {@link
      org.apache.log4j.rolling.TriggeringPolicy}.
      </p>
  
    </body> 
  </html>
  
  
  1.7       +27 -4     logging-log4j/src/java/org/apache/log4j/rolling/helpers/Compress.java
  
  Index: Compress.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/helpers/Compress.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Compress.java	27 Feb 2004 16:47:33 -0000	1.6
  +++ Compress.java	23 Nov 2004 16:30:14 -0000	1.7
  @@ -26,18 +26,41 @@
   
   
   /**
  - * @author Ceki
  - *
  + * The <code>Compression</code> class implements ZIP and GZ
  + * file compression/decompression methods.
  + * 
  + * @author Ceki G&uuml;lc&uuml;
  + * @since 1.3
    */
   public class Compress {
     static final Logger logger = Logger.getLogger(Compress.class);
     public static final int NONE = 0;
     public static final int GZ = 1;
     public static final int ZIP = 2;
  +  /**
  +   * String constant representing no compression. The value of this
  +   * constant is "NONE".
  +   */
     public static final String NONE_STR = "NONE";
  +
  +  /**
  +   * String constant representing compression in the GZIP format. The
  +   * value of this constant is "GZ".
  +   */
     public static final String GZ_STR = "GZ";
  +
  +  /**
  +   * String constant representing compression in the ZIP format. The
  +   * value of this constant is "ZIP".
  +   */
     public static final String ZIP_STR = "ZIP";
   
  +  public static void ZIPCompress(String nameOfFile2zip) {
  +    // Here we rely on the fact that the two argument version of ZIPCompress 
  +    // automaticallys add a .zip extension to the second argument 
  +    GZCompress(nameOfFile2zip, nameOfFile2zip);
  +  }
  +  
     public static void ZIPCompress(
       String nameOfFile2zip, String nameOfZippedFile) {
       File file2zip = new File(nameOfFile2zip);
  @@ -92,8 +115,8 @@
     }
   
     public static void GZCompress(String nameOfFile2gz) {
  -    // Here we rely on the fact that the two argument version of GZCompress automatically
  -    // add te .gz exention to the second argument 
  +    // Here we rely on the fact that the two argument version of GZCompress 
  +    // automatically adds a .gz extension to the second argument 
       GZCompress(nameOfFile2gz, nameOfFile2gz);
     }
   
  
  
  
  1.5       +1 -1      logging-log4j/src/java/org/apache/log4j/rolling/helpers/RollingCalendar.java
  
  Index: RollingCalendar.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/helpers/RollingCalendar.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RollingCalendar.java	28 May 2004 12:21:38 -0000	1.4
  +++ RollingCalendar.java	23 Nov 2004 16:30:14 -0000	1.5
  @@ -38,7 +38,7 @@
    *
    * */
   public class RollingCalendar extends GregorianCalendar {
  -  static final Logger logger = Logger.getLogger(RollingCalendar.class);
  +  final Logger logger = Logger.getLogger(RollingCalendar.class);
   
     // The gmtTimeZone is used only in computeCheckPeriod() method.
     static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org