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 WriterAppender.java Layout.java FileAppender.java RollingFileAppender.java
Date Fri, 28 May 2004 17:16:43 GMT
ceki        2004/05/28 10:16:43

  Modified:    tests/src/java/org/apache/log4j/rolling
                        TimeBasedRollingTest.java
               src/java/org/apache/log4j/rolling RollingFileAppender.java
               src/java/org/apache/log4j WriterAppender.java Layout.java
                        FileAppender.java RollingFileAppender.java
  Log:
  Simplified the actions taken when closing. This ensures that layout
  header and footer are written even in case of rolling files.
  
  This partially fixes bug 11446.
  
  However, in case a file is opened and closed multiple times, the header
  and footer get written multiple times as well.
  
  Revision  Changes    Path
  1.6       +15 -12    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TimeBasedRollingTest.java	27 Feb 2004 16:47:36 -0000	1.5
  +++ TimeBasedRollingTest.java	28 May 2004 17:16:42 -0000	1.6
  @@ -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.
  @@ -257,8 +257,9 @@
         filenames[i] = "output/test5-" + sdf.format(cal.getTime());
         cal.add(Calendar.SECOND, 1);
       }
  +
       filenames[3] = "output/test5.log";
  -    
  +
       root.debug("Waiting until next second and 100 millis.");
       delayUntilNextSecond(100);
       root.debug("Done waiting.");
  @@ -275,7 +276,7 @@
   
     /**
      * With compression, no stop/restart, activeFileName set
  -   * 
  +   *
      * The test is a regression test. It logs a few times using a RollingFileAppender.
      * It predicts the names of the files which will be generated and
      * compares them with witness files.
  @@ -297,11 +298,13 @@
       SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
       String[] filenames = new String[4];
   
  -    Calendar cal = Calendar.getInstance();  
  +    Calendar cal = Calendar.getInstance();
  +
       for (int i = 0; i < 3; i++) {
         filenames[i] = "output/test6-" + sdf.format(cal.getTime()) + ".gz";
         cal.add(Calendar.SECOND, 1);
       }
  +
       filenames[3] = "output/test6.log";
   
       root.debug("Waiting until next second and 100 millis.");
  @@ -320,14 +323,14 @@
       for (int i = 0; i < 3; i++) {
         Compare.gzCompare(filenames[i], "witness/tbr-test6." + i + ".gz");
       }
  +
       Compare.compare(filenames[3], "witness/tbr-test6.3");
     }
  -  
  -  
  +
     void delayUntilNextSecond(int millis) {
       long now = System.currentTimeMillis();
       Calendar cal = Calendar.getInstance();
  -	cal.setTime(new Date(now));
  +    cal.setTime(new Date(now));
   
       cal.set(Calendar.MILLISECOND, millis);
       cal.add(Calendar.SECOND, 1);
  @@ -343,12 +346,12 @@
     void delayUntilNextMinute(int seconds) {
       long now = System.currentTimeMillis();
       Calendar cal = Calendar.getInstance();
  -	cal.setTime(new Date(now));
  +    cal.setTime(new Date(now));
   
       cal.set(Calendar.SECOND, seconds);
       cal.add(Calendar.MINUTE, 1);
   
  -	long next = cal.getTime().getTime();
  +    long next = cal.getTime().getTime();
   
       try {
         Thread.sleep(next - now);
  @@ -363,7 +366,7 @@
       suite.addTest(new TimeBasedRollingTest("test2"));
       suite.addTest(new TimeBasedRollingTest("test3"));
       suite.addTest(new TimeBasedRollingTest("test4"));
  -    
  +
       suite.addTest(new TimeBasedRollingTest("test5"));
       suite.addTest(new TimeBasedRollingTest("test6"));
   
  
  
  
  1.15      +1 -1      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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- RollingFileAppender.java	27 Feb 2004 16:47:33 -0000	1.14
  +++ RollingFileAppender.java	28 May 2004 17:16:43 -0000	1.15
  @@ -86,7 +86,7 @@
     public void rollover() {
       // Note: synchronization not necessary since doAppend is already synched
       // make sure to close the hereto active log file!!
  -    this.closeFile();
  +    this.closeWriter();
   
       rollingPolicy.rollover();
   
  
  
  
  1.27      +29 -22    logging-log4j/src/java/org/apache/log4j/WriterAppender.java
  
  Index: WriterAppender.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/WriterAppender.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- WriterAppender.java	27 Feb 2004 16:47:28 -0000	1.26
  +++ WriterAppender.java	28 May 2004 17:16:43 -0000	1.27
  @@ -192,23 +192,26 @@
       }
   
       this.closed = true;
  -    writeFooter();
  -    reset();
  +    closeWriter();
     }
   
     /**
      * Close the underlying {@link java.io.Writer}.
      * */
     protected void closeWriter() {
  -    if (qw != null) {
  +    if (this.qw != null) {
         try {
  +        // before closing we have to output out layout's footer
  +        writeFooter();
           qw.close();
  +        this.qw = null;
         } catch (IOException e) {
           // There is do need to invoke an error handler at this late
           // stage.
           LogLog.error("Could not close " + qw, e);
         }
       }
  +    
     }
   
     /**
  @@ -274,7 +277,9 @@
       <p>
       @param writer An already opened Writer.  */
     public synchronized void setWriter(Writer writer) {
  -    reset();
  +    // close any previously opened writer
  +    closeWriter();
  +    
       this.qw = new QuietWriter(writer, errorHandler);
   
       //this.tp = new TracerPrintWriter(qw);
  @@ -282,12 +287,12 @@
     }
   
     /**
  -     Actual writing occurs here.
  -
  -     <p>Most subclasses of <code>WriterAppender</code> will need to
  -     override this method.
  -
  -     @since 0.9.0 */
  +   * Actual writing occurs here. 
  +   * <p>Most subclasses of <code>WriterAppender</code> will need to override

  +   * this method.
  +   *  
  +   * @since 0.9.0 
  +   * */
     protected void subAppend(LoggingEvent event) {
       this.qw.write(this.layout.format(event));
   
  @@ -318,20 +323,21 @@
     }
   
     /**
  -     Clear internal references to the writer and other variables.
  -
  -     Subclasses can override this method for an alternate closing
  -     behavior.  */
  -  protected void reset() {
  +   * Clear internal references to the writer and other variables.
  +   * 
  +   * Subclasses can override this method for an alternate closing 
  +   * behavior. 
  +   * 
  +   * @deprecated Use {@link #closeWriter} method instead.  
  +   * */
  +  protected void xreset() {
       closeWriter();
  -    this.qw = null;
  -
  -    //this.tp = null;
     }
   
     /**
  -     Write a footer as produced by the embedded layout's {@link
  -     Layout#getFooter} method.  */
  +   * Write a footer as produced by the embedded layout's {@link 
  +   * Layout#getFooter} method.  
  +   * */
     protected void writeFooter() {
       if (layout != null) {
         String f = layout.getFooter();
  @@ -344,8 +350,9 @@
     }
   
     /**
  -     Write a header as produced by the embedded layout's {@link
  -     Layout#getHeader} method.  */
  +   * Write a header as produced by the embedded layout's {@link 
  +   * Layout#getHeader} method.  
  +   * */
     protected void writeHeader() {
       if (layout != null) {
         String h = layout.getHeader();
  
  
  
  1.10      +38 -9     logging-log4j/src/java/org/apache/log4j/Layout.java
  
  Index: Layout.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/Layout.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Layout.java	27 Feb 2004 16:47:28 -0000	1.9
  +++ Layout.java	28 May 2004 17:16:43 -0000	1.10
  @@ -38,11 +38,17 @@
     public static final int LINE_SEP_LEN = LINE_SEP.length();
     
     final static Logger logger = Logger.getLogger(Layout.class); 
  -
  +  
  +  
     public CharArrayWriter charArrayWriter = new CharArrayWriter(1024);
  +
  +  String header;
  +  String footer;
  +
  +  
     /**
  -     Implement this method to create your own layout format.
  -  */
  +   * Implement this method to create your own layout format.
  +   * */
     public String format(LoggingEvent event) {
   	  charArrayWriter.reset();
   	  try {
  @@ -65,19 +71,20 @@
     }
   
     /**
  -     Returns the header for the layout format. The base class returns
  -     <code>null</code>.  */
  +   * Returns the header for the layout format. There is no default header.
  +   * */
     public String getHeader() {
  -    return null;
  +    return header;
     }
   
     /**
  -     Returns the footer for the layout format. The base class returns
  -     <code>null</code>.  */
  +   * Returns the footer for the layout format. There is no default footer.
  +   */
     public String getFooter() {
  -    return null;
  +    return footer;
     }
   
  +  
     /**
        If the layout handles the throwable object contained within
        {@link LoggingEvent}, then the layout should return
  @@ -90,4 +97,26 @@
   
        @since 0.8.4 */
     public abstract boolean ignoresThrowable();
  +  
  +  /**
  +   * Set the footer. Note that some layout have their own footers and may choose
  +   * to ignote the footer set here.
  +   * 
  +   * @param footer the footer
  +   * @since 1.3
  +   */
  +  public void setFooter(String footer) {
  +    this.footer = footer;
  +  }
  +
  +  /**
  +   * Set the header. Note that some layout have their own headers and may choose
  +   * to ignote the header set here.
  +   *
  +   * @param header the header
  +   * @since 1.3
  +   */
  +  public void setHeader(String header) {
  +    this.header = header;
  +  }
   }
  
  
  
  1.42      +5 -26     logging-log4j/src/java/org/apache/log4j/FileAppender.java
  
  Index: FileAppender.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/FileAppender.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- FileAppender.java	27 Feb 2004 16:47:28 -0000	1.41
  +++ FileAppender.java	28 May 2004 17:16:43 -0000	1.42
  @@ -162,18 +162,12 @@
     }
   
     /**
  -      Closes the previously opened file.
  +   * Closes the previously opened file.
  +   * 
  +   * @deprecated Use the super class' {@link #closeWriter} method instead.
      */
     protected void closeFile() {
  -    if (this.qw != null) {
  -      try {
  -        this.qw.close();
  -      } catch (java.io.IOException e) {
  -        // Exceptionally, it does not make sense to delegate to an
  -        // ErrorHandler. Since a closed appender is basically dead.
  -        LogLog.error("Could not close " + qw, e);
  -      }
  -    }
  +    closeWriter();
     }
   
     /**
  @@ -257,7 +251,7 @@
         setImmediateFlush(false);
       }
   
  -    reset();
  +    closeWriter();
   
       Writer fw = createWriter(new FileOutputStream(filename, append));
   
  @@ -281,20 +275,5 @@
      */
     protected void setQWForFiles(Writer writer) {
       this.qw = new QuietWriter(writer, errorHandler);
  -  }
  -
  -  /**
  -     Close any previously opened file and call the parent's
  -     <code>reset</code>.  */
  -  protected void reset() {
  -    closeFile();
  -
  -    // 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.20      +2 -1      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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- RollingFileAppender.java	27 Feb 2004 16:47:28 -0000	1.19
  +++ RollingFileAppender.java	28 May 2004 17:16:43 -0000	1.20
  @@ -140,7 +140,8 @@
         // Rename fileName to fileName.1
         target = new File(fileName + "." + 1);
   
  -      this.closeFile(); // keep windows happy.
  +      // Gracefully close the old the log file.
  +      this.closeWriter(); 
   
         file = new File(fileName);
         LogLog.debug("Renaming file " + file + " to " + target);
  
  
  

---------------------------------------------------------------------
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