ceki        2004/12/08 07:33:23

  Modified:    src/java/org/apache/log4j/rolling/helper
                        FileNamePattern.java IntegerTokenConverter.java
               tests/src/java/org/apache/log4j/rolling
                        SizeBasedRollingTest.java
  Added:       src/java/org/apache/log4j/rolling
                        FixedWindowRollingPolicy.java
  Removed:     src/java/org/apache/log4j/rolling
                        SlidingWindowRollingPolicy.java
  Log:
  - Renamed SlidingWindowRollingPolicy as FixedWindowRollingPolicy.
  - Improved documentation for FixedWindowRollingPolicy.
  
  Revision  Changes    Path
  1.4       +13 -0     
logging-log4j/src/java/org/apache/log4j/rolling/helper/FileNamePattern.java
  
  Index: FileNamePattern.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/helper/FileNamePattern.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileNamePattern.java      2 Dec 2004 09:11:52 -0000       1.3
  +++ FileNamePattern.java      8 Dec 2004 15:33:22 -0000       1.4
  @@ -173,6 +173,19 @@
       return null;
     }
   
  +  public IntegerTokenConverter getIntegerTokenConverter() {
  +    TokenConverter p = headTokenConverter;
  +
  +    while (p != null) {
  +      if (p.getType() == TokenConverter.INTEGER) {
  +        return (IntegerTokenConverter) p;
  +      }
  +
  +      p = p.getNext();
  +    }
  +    return null;
  +  }
  +  
     public String convert(int i) {
       TokenConverter p = headTokenConverter;
       StringBuffer buf = new StringBuffer();
  
  
  
  1.3       +1 -1      
logging-log4j/src/java/org/apache/log4j/rolling/helper/IntegerTokenConverter.java
  
  Index: IntegerTokenConverter.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/helper/IntegerTokenConverter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IntegerTokenConverter.java        23 Nov 2004 20:38:33 -0000      1.2
  +++ IntegerTokenConverter.java        8 Dec 2004 15:33:23 -0000       1.3
  @@ -23,7 +23,7 @@
    *  
    * @author Ceki Gulcu
    */
  -class IntegerTokenConverter extends TokenConverter {
  +public class IntegerTokenConverter extends TokenConverter {
     
     public IntegerTokenConverter() {
       super(TokenConverter.INTEGER);
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/log4j/rolling/FixedWindowRollingPolicy.java
  
  Index: FixedWindowRollingPolicy.java
  ===================================================================
  /*
   * 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.rolling.helper.Compress;
  import org.apache.log4j.rolling.helper.IntegerTokenConverter;
  import org.apache.log4j.rolling.helper.Util;
  
  import java.io.File;
  
  
  /**
   * When rolling over, <code>FixedWindowRollingPolicy</code> renames files 
   * according to a fixed window algorithm as described below. 
   * 
   * <p>The <b>ActiveFileName</b> property, which is required, represents the 
name 
   * of the file where current logging output will be written. 
   * The <b>FileNamePattern</b>  option represents the file name pattern for 
the 
   * archived (rolled over) log files. If present, the <b>FileNamePattern</b> 
   * option must include an integer token, that is the string "%i" somwhere 
   * within the pattern.
   * 
   * <p>Let <em>max</em> and <em>min</em> represent the values of respectively 
   * the <b>MaxIndex</b> and <b>MinIndex</b> options. Let "foo.log" be the value
   * of the <b>ActiveFile</b> option and "foo.%i.log" the value of 
   * <b>FileNamePattern</b>. Then, when rolling over, the file 
   * <code>foo.<em>max</em>.log</code> will be deleted, the file 
   * <code>foo.<em>max-1</em>.log</code> will be renamed as 
   * <code>foo.<em>max</em>.log</code>, the file 
<code>foo.<em>max-2</em>.log</code> 
   * renamed as <code>foo.<em>max-1</em>.log</code>, and so on, 
   * the file <code>foo.<em>min+1</em>.log</code> renamed as 
   * <code>foo.<em>min+2</em>.log</code>. Lastly, the active file 
<code>foo.log</code>
   * will be renamed as <code>foo.<em>min</em>.log</code> and a new active file 
name
   * <code>foo.log</code> will be created.
   * 
   * <p>Given that this rollover algorithm requires as many file renaming 
   * operations as the window size, large window sizes are discouraged. The
   * current implementation will automatically reduce the window size to 12 when
   * larger values are specified by the user.
   * 
   *
   * @author Ceki G&uuml;lc&uuml;
   * @since 1.3
   * */
  public class FixedWindowRollingPolicy extends RollingPolicySkeleton {
    int maxIndex;
    int minIndex;
    
    /**
     * It's almost always a bad idea to have a large window size, say over 12. 
     */
    private static int MAX_WINDOW_SIZE = 12;
    
    public FixedWindowRollingPolicy() {
      minIndex = 1;
      maxIndex = 7;
      activeFileName = null;
    }
  
    public void activateOptions() {
      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 ("
          + minIndex + ").");
        getLogger().warn("Setting maxIndex to equal minIndex.");
        maxIndex = minIndex;
      }
      
      if((maxIndex-minIndex) > MAX_WINDOW_SIZE) {
        getLogger().warn("Large window sizes are not allowed.");
        maxIndex = minIndex +  MAX_WINDOW_SIZE;
        getLogger().warn("MaxIndex reduced to {}.", new Integer(maxIndex));
      }
      
      if (fileNamePatternStr != null) {
        determineCompressionMode();
      }
      
      IntegerTokenConverter itc = fileNamePattern.getIntegerTokenConverter();
  
      if (itc == null) {
        throw new IllegalStateException(
          "FileNamePattern [" + fileNamePattern.getPattern()
          + "] does not contain a valid IntegerToken");
      }
    }
  
    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.
      if (maxIndex >= 0) {
        // Delete the oldest file, to keep Windows happy.
        File file = new File(fileNamePattern.convert(maxIndex));
  
        if (file.exists()) {
          file.delete();
        }
  
        // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1}
        for (int i = maxIndex - 1; i >= minIndex; i--) {
          
          Util.rename(
            fileNamePattern.convert(i), fileNamePattern.convert(i + 1));
        }
  
        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;
          
          case Compress.ZIP:
          Compress.ZIPCompress(
            activeFileName, fileNamePattern.convert(minIndex));
          break;
          }
        }
      }
    }
  
    /**
     * Return the value of the <b>ActiveFile</b> option.
     * 
     * @see [EMAIL PROTECTED] setActiveFileName}.
    */
    public String getActiveFileName() {
      // TODO This is clearly bogus.
      return activeFileName;
    }
  
    public int getMaxIndex() {
      return maxIndex;
    }
  
    public int getMinIndex() {
      return minIndex;
    }
  
    public void setMaxIndex(int maxIndex) {
      this.maxIndex = maxIndex;
    }
  
    public void setMinIndex(int minIndex) {
      this.minIndex = minIndex;
    }
  
  }
  
  
  
  1.5       +3 -3      
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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SizeBasedRollingTest.java 23 Nov 2004 16:30:13 -0000      1.4
  +++ SizeBasedRollingTest.java 8 Dec 2004 15:33:23 -0000       1.5
  @@ -61,7 +61,7 @@
       RollingFileAppender rfa = new RollingFileAppender();
       rfa.setLayout(layout);
   
  -    SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
  +    FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
       SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
       sbtp.setMaxFileSize(100);
       sbtp.activateOptions();
  @@ -82,7 +82,7 @@
       RollingFileAppender rfa = new RollingFileAppender();
       rfa.setLayout(layout);
   
  -    SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
  +    FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
       SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
   
       sbtp.setMaxFileSize(100);
  @@ -126,7 +126,7 @@
        RollingFileAppender rfa = new RollingFileAppender();
        rfa.setLayout(layout);
   
  -     SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
  +     FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
        SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
   
        //swrp.setCompressionMode("GZ");
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to