logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c...@apache.org
Subject cvs commit: jakarta-log4j/tests/src/java/org/apache/log4j/rolling SizeBasedRollingTestCase.java
Date Fri, 16 May 2003 11:36:20 GMT
ceki        2003/05/16 04:36:20

  Modified:    src/java/org/apache/log4j/rolling RollingPolicy.java
                        RollingFileAppender.java TriggeringPolicy.java
               tests    build.xml
               src/java/org/apache/log4j FileAppender.java
  Added:       src/java/org/apache/log4j/rolling
                        SizeBasedTriggeringPolicy.java
               tests/src/java/org/apache/log4j/rolling
                        SizeBasedRollingTestCase.java
  Log:
  The first edition of sized based rolling policy
  
  Revision  Changes    Path
  1.4       +3 -1      jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java
  
  Index: RollingPolicy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RollingPolicy.java	14 May 2003 18:20:28 -0000	1.3
  +++ RollingPolicy.java	16 May 2003 11:36:19 -0000	1.4
  @@ -61,8 +61,10 @@
   
   import java.io.File;
   
  +import org.apache.log4j.spi.OptionHandler;
   
  -public interface RollingPolicy {
  +
  +public interface RollingPolicy extends OptionHandler {
       
     /**
      * Copy the file passed as parameter to an appropriate location.
  
  
  
  1.6       +44 -23    jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java
  
  Index: RollingFileAppender.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RollingFileAppender.java	14 May 2003 18:20:28 -0000	1.5
  +++ RollingFileAppender.java	16 May 2003 11:36:19 -0000	1.6
  @@ -50,12 +50,14 @@
   package org.apache.log4j.rolling;
   
   import org.apache.log4j.FileAppender;
  +import org.apache.log4j.Logger;
   import org.apache.log4j.helpers.LogLog;
   import org.apache.log4j.spi.LoggingEvent;
   
   import java.io.File;
   import java.io.IOException;
   
  +
   /**
    * RollingFileAppender extends FileAppender to backup the log files
    * depending on rotation policy.
  @@ -65,7 +67,9 @@
    * @since  1.3
    * */
   public class RollingFileAppender extends FileAppender {
  -  File file;
  +  
  +  Logger logger = Logger.getLogger(RollingFileAppender.class);
  +  File activeFile;
     TriggeringPolicy triggeringPolicy;
     RollingPolicy rollingPolicy;
   
  @@ -77,6 +81,23 @@
       super();
     }
   
  +  public void activateOptions() {
  +    
  +    if(triggeringPolicy == null) {
  +      logger.warn("Please set a TriggeringPolicy for ");
  +      return;
  +    }
  +    if (rollingPolicy != null) {
  +      rollingPolicy.activateOptions();
  +
  +      setFile(rollingPolicy.getActiveLogFileName());
  +      activeFile = new File(rollingPolicy.getActiveLogFileName());
  +      super.activateOptions();
  +    } else {
  +      logger.warn("Please set a rolling policy");
  +    }
  +  }
  +
     /**
        Implements the usual roll over behaviour.
   
  @@ -93,13 +114,11 @@
      */
     public void rollover() {
       // Note: synchronization not necessary since doAppend is already synched
  -      
  -      // make sure to close the hereto active log file!!
  -      this.closeFile(); 
  -            
  -      rollingPolicy.rollover(file);
  +    // make sure to close the hereto active log file!!
  +    this.closeFile();
   
  -      File activeFile = new File(rollingPolicy.getActiveLogFileName());
  +    activeFile = new File(fileName);
  +    rollingPolicy.rollover(activeFile);
   
       try {
         // This will also close the file. This is OK since multiple
  @@ -110,30 +129,32 @@
       }
     }
   
  -  public synchronized void setFile(
  -    String fileName, boolean append, boolean bufferedIO, int bufferSize)
  -    throws IOException {
  -    super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
  -    file = new File(fileName);
  -  }
  -
     /**
        This method differentiates RollingFileAppender from its super
        class.
     */
     protected void subAppend(LoggingEvent event) {
       super.subAppend(event);
  +    
  +    if (triggeringPolicy.isTriggeringEvent(activeFile)) {
  +      System.out.println("About to rollover");
  +      rollover();
  +    }
  +  }
   
  -    boolean trigger;
  +  public RollingPolicy getRollingPolicy() {
  +    return rollingPolicy;
  +  }
   
  -    if (triggeringPolicy.isSizeSensitive()) {
  -      trigger = triggeringPolicy.isTriggeringEvent(file.length());
  -    } else {
  -      trigger = triggeringPolicy.isTriggeringEvent();
  -    }
  +  public TriggeringPolicy getTriggeringPolicy() {
  +    return triggeringPolicy;
  +  }
   
  -    if (trigger) {
  -      rollover();
  -    }
  +  public void setRollingPolicy(RollingPolicy policy) {
  +    rollingPolicy = policy;
  +  }
  +
  +  public void setTriggeringPolicy(TriggeringPolicy policy) {
  +    triggeringPolicy = policy;
     }
   }
  
  
  
  1.2       +60 -12    jakarta-log4j/src/java/org/apache/log4j/rolling/TriggeringPolicy.java
  
  Index: TriggeringPolicy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/TriggeringPolicy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TriggeringPolicy.java	22 Oct 2002 21:29:28 -0000	1.1
  +++ TriggeringPolicy.java	16 May 2003 11:36:20 -0000	1.2
  @@ -1,27 +1,75 @@
   /*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  + * ============================================================================
  + *                   The Apache Software License, Version 1.1
  + * ============================================================================
    *
  - * This software is published under the terms of the Apache Software
  - * License version 1.1, a copy of which has been included with this
  - * distribution in the LICENSE.txt file.  */
  + *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without modifica-
  + * tion, are permitted provided that the following conditions are met:
  + *
  + * 1. Redistributions of  source code must  retain the above copyright  notice,
  + *    this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright notice,
  + *    this list of conditions and the following disclaimer in the documentation
  + *    and/or other materials provided with the distribution.
  + *
  + * 3. The end-user documentation included with the redistribution, if any, must
  + *    include  the following  acknowledgment:  "This product includes  software
  + *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
  + *    Alternately, this  acknowledgment may  appear in the software itself,  if
  + *    and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
  + *    endorse  or promote  products derived  from this  software without  prior
  + *    written permission. For written permission, please contact
  + *    apache@apache.org.
  + *
  + * 5. Products  derived from this software may not  be called "Apache", nor may
  + *    "Apache" appear  in their name,  without prior written permission  of the
  + *    Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  + * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
  + * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
  + * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
  + * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
  + * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
  + * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
  + * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
  + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  + *
  + * This software  consists of voluntary contributions made  by many individuals
  + * on  behalf of the Apache Software  Foundation.  For more  information on the
  + * Apache Software Foundation, please see <http://www.apache.org/>.
  + *
  + */
  +
   
   /**
    * A <code>TriggeringPolicy</code> determines the time where rollover
  - * should occur.  
  + * should occur.
    *
    * @author Ceki G&uuml;lc&uuml;
    * */
   package org.apache.log4j.rolling;
   
  -public interface TriggeringPolicy {
  -  
  -  /** 
  +import java.io.File;
  +
  +import org.apache.log4j.spi.OptionHandler;
  +
  +public interface TriggeringPolicy extends OptionHandler {
  +  /**
      * Should rolllover be triggered at this time?
  +   * 
  +   * A reference to the active log file is supplied as a parameter.
  +   * 
      * */
  -  public boolean isTriggeringEvent(long size);
  -
  -  public boolean isTriggeringEvent();
  +  public boolean isTriggeringEvent(File file);
   
  -  public boolean isSizeSensitive();
  +  //public boolean isTriggeringEvent();
   
  +  //public boolean isSizeSensitive();
   }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/SizeBasedTriggeringPolicy.java
  
  Index: SizeBasedTriggeringPolicy.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling;
  
  import java.io.File;
  
  
  /**
   * @author Ceki G&uuml;lc&uuml;
   *
   * To change the template for this generated type comment go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  public class SizeBasedTriggeringPolicy implements TriggeringPolicy {
    long maxFileSize = 10 * 1024 * 1024; // let 10 MB the default max size
  
    public boolean isTriggeringEvent(File file) {
      return (file.length() > maxFileSize);
    }
  
    public long getMaxFileSize() {
      return maxFileSize;
    }
  
    public void setMaxFileSize(long l) {
      maxFileSize = l;
    }
    
    public void activateOptions() {
     
    }
  }
  
  
  
  1.27      +8 -0      jakarta-log4j/tests/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/tests/build.xml,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- build.xml	3 Mar 2003 15:24:03 -0000	1.26
  +++ build.xml	16 May 2003 11:36:20 -0000	1.27
  @@ -371,6 +371,14 @@
       </junit>
     </target>
   
  +  <target name="FileNamePatternParser" depends="build, cleanOutputDir">
  +    <junit printsummary="yes" fork="yes" haltonfailure="yes">
  +      <classpath refid="tests.classpath"/>
  +      <formatter type="plain" usefile="false"/>
  +      <test name="org.apache.log4j.rolling.helpers.FileNamePatternParserTestCase" />
  +    </junit>
  +  </target>
  +
     <!-- ================================================================= -->
     <!-- ========================= Very long Tests ======================= -->
     <!-- ================================================================= -->
  
  
  
  1.37      +12 -4     jakarta-log4j/src/java/org/apache/log4j/FileAppender.java
  
  Index: FileAppender.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/FileAppender.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- FileAppender.java	18 Mar 2003 21:17:42 -0000	1.36
  +++ FileAppender.java	16 May 2003 11:36:20 -0000	1.37
  @@ -54,6 +54,7 @@
   import org.apache.log4j.spi.ErrorCode;
   
   import java.io.BufferedWriter;
  +import java.io.File;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.Writer;
  @@ -184,6 +185,7 @@
         try {
           setFile(fileName, fileAppend, bufferedIO, bufferSize);
         } catch (java.io.IOException e) {
  +        System.out.println("xx" + fileName);
           errorHandler.error(
             "setFile(" + fileName + "," + fileAppend + ") call failed.", e,
             ErrorCode.FILE_OPEN_FAILURE);
  @@ -282,7 +284,7 @@
       @param append   If true will append to fileName. Otherwise will
           truncate fileName.  */
     public synchronized void setFile(
  -    String fileName, boolean append, boolean bufferedIO, int bufferSize)
  +    String filename, boolean append, boolean bufferedIO, int bufferSize)
       throws IOException {
       LogLog.debug("setFile called: " + fileName + ", " + append);
   
  @@ -293,16 +295,16 @@
   
       reset();
   
  -    Writer fw = createWriter(new FileOutputStream(fileName, append));
  +    Writer fw = createWriter(new FileOutputStream(filename, append));
   
       if (bufferedIO) {
         fw = new BufferedWriter(fw, bufferSize);
       }
   
       this.setQWForFiles(fw);
  -    this.fileName = fileName;
       this.fileAppend = append;
       this.bufferedIO = bufferedIO;
  +    this.fileName = filename;
       this.bufferSize = bufferSize;
       writeHeader();
       LogLog.debug("setFile ended");
  @@ -322,7 +324,13 @@
        <code>reset</code>.  */
     protected void reset() {
       closeFile();
  -    this.fileName = null;
  +
  +    // The following line is commented out. It causes problems with the setFile
  +    // method taking 4 params which calls the reset method. If there is a 
  +    // problem with the createWrite call then the fileName never gets set properly.
  +    
  +    // this.fileName = null;
  +    
       super.reset();
     }
   }
  
  
  
  1.1                  jakarta-log4j/tests/src/java/org/apache/log4j/rolling/SizeBasedRollingTestCase.java
  
  Index: SizeBasedRollingTestCase.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.apache.log4j.ConsoleAppender;
  import org.apache.log4j.Logger;
  import org.apache.log4j.PatternLayout;
  
  
  /**
   *
   * @author Ceki G&uuml;lc&uuml;
   *
   */
  public class SizeBasedRollingTestCase extends TestCase {
    Logger logger = Logger.getLogger(SizeBasedRollingTestCase.class);
  
    public SizeBasedRollingTestCase(String name) {
      super(name);
    }
  
    public void setUp() {
    }
  
    public void tearDown() {
    }
  
    public void test1() {
      Logger root = Logger.getRootLogger();
      root.addAppender(new ConsoleAppender(new PatternLayout()));
      
      PatternLayout layout = new PatternLayout("%m%n");
      RollingFileAppender rfa = new RollingFileAppender();
      rfa.setLayout(layout);
      SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
      SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
      sbtp.setMaxFileSize(1000);
      swrp.setFileNamePattern("output/test.%i");
      rfa.setRollingPolicy(swrp);
      rfa.setTriggeringPolicy(sbtp);
      //rfa.setFile("test");
      rfa.activateOptions();
      logger.addAppender(rfa);
      
  
      for (int i = 0; i < 1000; i++) {
        try {
          Thread.sleep(10);
        } catch(Exception e) {}
        if (i < 10) {
          logger.debug("Hello   " + i);
        } else {
          logger.debug("Hello  " + i);
        }
      }
    }
  
    public static Test suite() {
      TestSuite suite = new TestSuite();
      suite.addTest(new SizeBasedRollingTestCase("test1"));
  
      return suite;
    }
  }
  
  
  

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


Mime
View raw message