logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c...@apache.org
Subject cvs commit: logging-log4j/src/java/org/apache/log4j/rolling/helpers Util.java
Date Mon, 31 May 2004 21:29:15 GMT
ceki        2004/05/31 14:29:15

  Modified:    src/java/org/apache/log4j/rolling RollingPolicy.java
                        RollingFileAppender.java
                        SlidingWindowRollingPolicy.java
                        RollingPolicySkeleton.java
                        TimeBasedRollingPolicy.java
               src/java/org/apache/log4j/rolling/helpers Util.java
  Added:       src/java/org/apache/log4j/rolling RolloverFailure.java
  Log:
  Improved behavior of org.apache.log4j.rolling.RollingFileAppender in
  case the file to be rolled over has been already opened by another
  process. This has been a source bugs under Windows.
  
  Revision  Changes    Path
  1.7       +1 -1      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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- RollingPolicy.java	27 Feb 2004 16:47:33 -0000	1.6
  +++ RollingPolicy.java	31 May 2004 21:29:15 -0000	1.7
  @@ -37,7 +37,7 @@
      * Rollover log files according to implementation policy.
      * .
      * */
  -  public void rollover();
  +  public void rollover() throws RolloverFailure;
   
     /**
      * Get the new name of the active log file.
  
  
  
  1.17      +21 -7     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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- RollingFileAppender.java	28 May 2004 19:30:17 -0000	1.16
  +++ RollingFileAppender.java	31 May 2004 21:29:15 -0000	1.17
  @@ -18,6 +18,7 @@
   
   import org.apache.log4j.FileAppender;
   import org.apache.log4j.Logger;
  +import org.apache.log4j.helpers.LogLog;
   import org.apache.log4j.spi.ErrorCode;
   import org.apache.log4j.spi.LoggingEvent;
   
  @@ -86,12 +87,25 @@
   
      */
     public void rollover() {
  -    // Note: synchronization not necessary since doAppend is already synched
  -    // make sure to close the hereto active log file!!
  -    this.closeWriter();
  -
  -    rollingPolicy.rollover();
  -
  +    // Note: synchronization at this point is unnecessary as the doAppend 
  +    // is already synched
  +    
  +    //
  +    // make sure to close the hereto active log file! Renaming under windows
  +    // does not work for open files.
  +    this.closeWriter();    
  +    
  +    // By default, the newly created file will be created in truncate mode.
  +    // (See the setFile(fileName,...) call a few lines below.)
  +    boolean append = false;
  +    try { 
  +      rollingPolicy.rollover();
  +    } catch(RolloverFailure rf) {
  +      LogLog.warn("RolloverFailure occurred. Deferring rollover.");
  +      // we failed to rollover, let us not truncate and risk data loss
  +      append = true;
  +    }
  +    
       // Although not certain, the active file name may change after roll over.
       fileName = rollingPolicy.getActiveLogFileName();
       logger.debug("Active file name is now ["+fileName+"].");
  @@ -102,7 +116,7 @@
       try {
         // This will also close the file. This is OK since multiple
         // close operations are safe.
  -      this.setFile(fileName, false, bufferedIO, bufferSize);
  +      this.setFile(fileName, append, bufferedIO, bufferSize);
       } catch (IOException e) {
         errorHandler.error(
           "setFile(" + fileName + ", false) call failed.", e,
  
  
  
  1.12      +1 -2      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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SlidingWindowRollingPolicy.java	28 May 2004 19:30:17 -0000	1.11
  +++ SlidingWindowRollingPolicy.java	31 May 2004 21:29:15 -0000	1.12
  @@ -17,7 +17,6 @@
   package org.apache.log4j.rolling;
   
   import org.apache.log4j.Logger;
  -import org.apache.log4j.helpers.LogLog;
   import org.apache.log4j.rolling.helpers.Compress;
   import org.apache.log4j.rolling.helpers.FileNamePattern;
   import org.apache.log4j.rolling.helpers.Util;
  @@ -47,7 +46,7 @@
       activeFileName = null;
     }
   
  -  public void rollover() {
  +  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) {
  
  
  
  1.4       +0 -9      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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RollingPolicySkeleton.java	27 Feb 2004 16:47:33 -0000	1.3
  +++ RollingPolicySkeleton.java	31 May 2004 21:29:15 -0000	1.4
  @@ -24,15 +24,6 @@
   abstract public class RollingPolicySkeleton implements RollingPolicy {
     int compressionMode = Compress.NONE;
     
  -  /* (non-Javadoc)
  -   * @see org.apache.log4j.rolling.RollingPolicy#rollover()
  -   */
  -  abstract public void rollover();
  -  
  -  /* (non-Javadoc)
  -   * @see org.apache.log4j.rolling.RollingPolicy#getActiveLogFileName()
  -   */
  -  abstract public String getActiveLogFileName();
     
     /* (non-Javadoc)
      * @see org.apache.log4j.spi.OptionHandler#activateOptions()
  
  
  
  1.7       +28 -20    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TimeBasedRollingPolicy.java	28 May 2004 19:30:17 -0000	1.6
  +++ TimeBasedRollingPolicy.java	31 May 2004 21:29:15 -0000	1.7
  @@ -30,7 +30,7 @@
   
   /**
    *
  - *
  + * 
    * If configuring programatically, do not forget to call {@link #activateOptions}
    * method before using this policy.
    * 
  @@ -92,25 +92,7 @@
         //logger.debug("Next check set to: " + nc);  
     }
   
  -
  -  /**
  -   * 
  -   * 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.
  -   *  
  -   */
  -  public String getActiveLogFileName() {
  -    logger.debug("getActiveLogFileName called");
  -    if (activeFileName == null) {
  -      return fileNamePattern.convert(now);
  -    } else {
  -      return activeFileName;
  -    }
  -  }
  -
  -  public void rollover() {
  +  public void rollover() throws RolloverFailure {
       logger.debug("rollover called");
       logger.debug("compressionMode: " + compressionMode);
   
  @@ -152,6 +134,23 @@
       }
     }
   
  +  /**
  +   * 
  +   * 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");
  +    if (activeFileName == null) {
  +      return fileNamePattern.convert(now);
  +    } else {
  +      return activeFileName;
  +    }
  +  }
  +
     public void setFileNamePattern(String fnp) {
       fileNamePatternStr = fnp;
     }
  @@ -183,10 +182,19 @@
       }
     }
   
  +  /**
  +   * 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/RolloverFailure.java
  
  Index: RolloverFailure.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;
  
  
  /**
   * A RolloverFailure occurs if, for whatever reason a rollover fails.
   *
   * @author Ceki Gulcu
   */
  public class RolloverFailure extends Exception {
  
    public RolloverFailure(String msg) {
      super(msg);
    }
  }
  
  
  
  1.3       +39 -4     logging-log4j/src/java/org/apache/log4j/rolling/helpers/Util.java
  
  Index: Util.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/helpers/Util.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Util.java	27 Feb 2004 16:47:33 -0000	1.2
  +++ Util.java	31 May 2004 21:29:15 -0000	1.3
  @@ -17,8 +17,13 @@
   package org.apache.log4j.rolling.helpers;
   
   import org.apache.log4j.Logger;
  +import org.apache.log4j.helpers.LogLog;
  +import org.apache.log4j.rolling.RolloverFailure;
   
   import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
   
   
   /**
  @@ -28,9 +33,10 @@
   public class Util {
     static Logger logger = Logger.getLogger(Util.class);
   
  -  public static void rename(String from, String to) {
  +  public static void rename(String from, String to) throws RolloverFailure {
       File fromFile = new File(from);
  -
  +    boolean success = false;
  +    
       if (fromFile.exists()) {
         File toFile = new File(to);
         logger.debug("Renaming file [" + fromFile + "] to [" + toFile + "]");
  @@ -38,9 +44,38 @@
         boolean result = fromFile.renameTo(toFile);
   
         if (!result) {
  -        logger.debug(
  -          "Failed to rename file [" + fromFile + "] to [" + toFile + "].");
  +        String msg1 = "Failed to rename file [" + fromFile + "] to [" + toFile + "].";
  +        LogLog.warn(msg1);
  +        LogLog.warn("Attemting to rename by copying.");
  +        renameByCopying(from, to);
         }
  +    } else {
  +      throw new RolloverFailure("File ["+from+"] does not exist.");
  +    }
  +  }
  +  
  +  public static void renameByCopying(String from, String to) throws RolloverFailure {
  +    try {
  +     FileInputStream fis = new FileInputStream(from);
  +     FileOutputStream fos = new FileOutputStream(to);
  +     byte[] inbuf = new byte[8102];
  +     int n;
  +
  +     while ((n = fis.read(inbuf)) != -1) {
  +       fos.write(inbuf, 0, n);
  +     }
  +
  +     fis.close();
  +     fos.close();
  +     
  +     File fromFile = new File(from);
  +     
  +     if (!fromFile.delete()) {
  +       logger.warn("Could not delete [" + from + "].");
  +     }
  +    } catch (IOException ioe) {
  +      LogLog.error("Failed to rename file by copying", ioe);
  +      throw new RolloverFailure("Failed to rename file by copying");
       }
     }
   }
  
  
  

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


Mime
View raw message