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 ca...@apache.org on 2004/12/21 05:19:47 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/helpers AbsoluteTimeDateFormat.java DateTimeDateFormat.java ISO8601DateFormat.java RelativeTimeDateFormat.java

carnold     2004/12/20 20:19:47

  Modified:    src/java/org/apache/log4j PatternLayout.java
               src/java/org/apache/log4j/helpers
                        AbsoluteTimeDateFormat.java DateTimeDateFormat.java
                        ISO8601DateFormat.java RelativeTimeDateFormat.java
  Log:
  Bug 32064: Simplify obsolete and buggy DateFormats
  
  Revision  Changes    Path
  1.33      +9 -27     logging-log4j/src/java/org/apache/log4j/PatternLayout.java
  
  Index: PatternLayout.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/PatternLayout.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- PatternLayout.java	22 Nov 2004 20:21:00 -0000	1.32
  +++ PatternLayout.java	21 Dec 2004 04:19:46 -0000	1.33
  @@ -31,7 +31,7 @@
   /**
     * <p>A flexible layout configurable with pattern string. The goal of this class 
     * is to {@link #format format} a {@link LoggingEvent} and return the results 
  -  * in a {#link StringBuffer}. The format of the result depensd on the 
  +  * in a {#link StringBuffer}. The format of the result depends on the 
     * <em>conversion pattern</em>.
     * <p>
     * 
  @@ -121,31 +121,13 @@
   
      <tr> <td align=center><b>d</b></td> <td>Used to output the date of
            the logging event. The date conversion specifier may be
  -         followed by a <em>date format specifier</em> enclosed between
  -         braces. For example, <b>%d{HH:mm:ss,SSS}</b> or
  -         <b>%d{dd&nbsp;MMM&nbsp;yyyy&nbsp;HH:mm:ss,SSS}</b>.  If no
  -         date format specifier is given then ISO8601 format is
  -         assumed.
  -
  -         <p>The date format specifier admits the same syntax as the
  -         time pattern string of the {@link
  -         java.text.SimpleDateFormat}. Although part of the standard
  -         JDK, the performance of <code>SimpleDateFormat</code> is
  -         quite poor.
  -
  -         <p>For better results it is recommended to use the log4j date
  -         formatters. These can be specified using one of the strings
  -         "ABSOLUTE", "DATE" and "ISO8601" for specifying {@link
  -         org.apache.log4j.helpers.AbsoluteTimeDateFormat
  -         AbsoluteTimeDateFormat}, {@link
  -         org.apache.log4j.helpers.DateTimeDateFormat DateTimeDateFormat}
  -         and respectively {@link
  -         org.apache.log4j.helpers.ISO8601DateFormat
  -         ISO8601DateFormat}. For example, <b>%d{ISO8601}</b> or
  -         <b>%d{ABSOLUTE}</b>.
  -
  -         <p>These dedicated date formatters perform significantly
  -         better than {@link java.text.SimpleDateFormat}.
  +         followed by a set of braces containing a
  +         date and time pattern strings {@link java.text.SimpleDateFormat}, 
  +         <em>ABSOLUTE</em>, <em>DATE</em> or <em>ISO8601</em>.
  +         For example, <b>%d{HH:mm:ss,SSS}</b>,
  +         <b>%d{dd&nbsp;MMM&nbsp;yyyy&nbsp;HH:mm:ss,SSS}</b> or
  +         <b>%d{DATE}</b>.  If no date format specifier is given then
  +         ISO8601 format is assumed.
        </td>
      </tr>
   
  @@ -156,7 +138,7 @@
      issued.
   
      <p><b>WARNING</b> Generating caller location information is
  -   extremely slow. It's use should be avoided unless execution speed
  +   extremely slow. Its use should be avoided unless execution speed
      is not an issue.
   
      </tr>
  
  
  
  1.5       +32 -95    logging-log4j/src/java/org/apache/log4j/helpers/AbsoluteTimeDateFormat.java
  
  Index: AbsoluteTimeDateFormat.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/AbsoluteTimeDateFormat.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbsoluteTimeDateFormat.java	17 Feb 2002 21:50:58 -0000	1.4
  +++ AbsoluteTimeDateFormat.java	21 Dec 2004 04:19:47 -0000	1.5
  @@ -1,19 +1,23 @@
   /*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  - *
  - * 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 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.helpers;
   
  -import java.util.Date;
  -import java.util.Calendar;
   import java.util.TimeZone;
  -import java.text.FieldPosition;
  -import java.text.ParsePosition;
  -import java.text.DateFormat;
  -
  +import java.text.SimpleDateFormat;
   
   /**
      Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
  @@ -24,111 +28,44 @@
   
      @since 0.7.5
   */
  -public class AbsoluteTimeDateFormat extends DateFormat {
  +public class AbsoluteTimeDateFormat extends SimpleDateFormat {
   
     /**
        String constant used to specify {@link
        org.apache.log4j.helpers.AbsoluteTimeDateFormat} in layouts. Current
        value is <b>ABSOLUTE</b>.  */
  -  public final static String ABS_TIME_DATE_FORMAT = "ABSOLUTE";
  +  public static final String ABS_TIME_DATE_FORMAT = "ABSOLUTE";
   
     /**
        String constant used to specify {@link
        org.apache.log4j.helpers.DateTimeDateFormat} in layouts.  Current
        value is <b>DATE</b>.
     */
  -  public final static String DATE_AND_TIME_DATE_FORMAT = "DATE";
  +  public static final String DATE_AND_TIME_DATE_FORMAT = "DATE";
   
     /**
        String constant used to specify {@link
        org.apache.log4j.helpers.ISO8601DateFormat} in layouts. Current
        value is <b>ISO8601</b>.
     */
  -  public final static String ISO8601_DATE_FORMAT = "ISO8601";
  -
  -  public
  -  AbsoluteTimeDateFormat() {
  -    setCalendar(Calendar.getInstance());
  -  }
  -  
  -  public
  -  AbsoluteTimeDateFormat(TimeZone timeZone) {
  -    setCalendar(Calendar.getInstance(timeZone));
  -  }
  -
  -  private static long   previousTime;
  -  private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
  +  public static final String ISO8601_DATE_FORMAT = "ISO8601";
   
     /**
  -     Appends to <code>sbuf</code> the time in the format
  -     "HH:mm:ss,SSS" for example, "15:49:37,459"
  -
  -     @param date the date to format
  -     @param sbuf the string buffer to write to
  -     @param fieldPosition remains untouched
  -    */
  -  public
  -  StringBuffer format(Date date, StringBuffer sbuf,
  -		      FieldPosition fieldPosition) {
  -
  -    long now = date.getTime();
  -    int millis = (int)(now % 1000);
  -
  -    if ((now - millis) != previousTime) {
  -      // We reach this point at most once per second
  -      // across all threads instead of each time format()
  -      // is called. This saves considerable CPU time.
  -
  -      calendar.setTime(date);
  -
  -      int start = sbuf.length();
  -      
  -      int hour = calendar.get(Calendar.HOUR_OF_DAY);
  -      if(hour < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(hour);
  -      sbuf.append(':');
  -      
  -      int mins = calendar.get(Calendar.MINUTE);
  -      if(mins < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(mins);
  -      sbuf.append(':');
  -      
  -      int secs = calendar.get(Calendar.SECOND);
  -      if(secs < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(secs);
  -      sbuf.append(',');      
  -
  -      // store the time string for next time to avoid recomputation
  -      sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
  -      
  -      previousTime = now - millis;
  -    }
  -    else {
  -      sbuf.append(previousTimeWithoutMillis);
  -    }
  -    
  -
  -    
  -    if(millis < 100) 
  -      sbuf.append('0');
  -    if(millis < 10) 
  -      sbuf.append('0');
  -    
  -    sbuf.append(millis);
  -    return sbuf;
  +   *  Constructs a AbsoluteTimeDateFormat using the default locale and time zone.
  +   */
  +  public AbsoluteTimeDateFormat() {
  +    super("HH:mm:ss,SSS");
     }
   
     /**
  -     This method does not do anything but return <code>null</code>.
  +   *  Constructs a AbsoluteTimeDateFormat using the default locale and 
  +   *      a given time zone.
  +   *
  +   *  @param timeZone the given time zone.
      */
  -  public
  -  Date parse(String s, ParsePosition pos) {
  -    return null;
  -  }  
  +  public AbsoluteTimeDateFormat(final TimeZone timeZone) {
  +    super("HH:mm:ss,SSS");
  +    setTimeZone(timeZone);
  +  }
  +
   }
  
  
  
  1.5       +28 -53    logging-log4j/src/java/org/apache/log4j/helpers/DateTimeDateFormat.java
  
  Index: DateTimeDateFormat.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/DateTimeDateFormat.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DateTimeDateFormat.java	24 Apr 2002 01:16:13 -0000	1.4
  +++ DateTimeDateFormat.java	21 Dec 2004 04:19:47 -0000	1.5
  @@ -1,18 +1,23 @@
   /*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  - *
  - * 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 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.helpers;
   
  -import java.util.Calendar;
   import java.util.TimeZone;
  -import java.util.Date;
  -import java.text.FieldPosition;
  -import java.text.ParsePosition;
  -import java.text.DateFormatSymbols;
  +import java.text.SimpleDateFormat;
   
   /**
      Formats a {@link Date} in the format "dd MMM YYYY HH:mm:ss,SSS" for example,
  @@ -21,54 +26,24 @@
      @author Ceki G&uuml;lc&uuml;
      @since 0.7.5
   */
  -public class DateTimeDateFormat extends AbsoluteTimeDateFormat {
  -
  -  String[] shortMonths;
  -
  -  public
  -  DateTimeDateFormat() {
  -    super();
  -    shortMonths = new DateFormatSymbols().getShortMonths();
  -  }
  -
  -  public
  -  DateTimeDateFormat(TimeZone timeZone) {
  -    this();
  -    setCalendar(Calendar.getInstance(timeZone));
  -  }
  +public class DateTimeDateFormat extends SimpleDateFormat {
   
     /**
  -     Appends to <code>sbuf</code> the date in the format "dd MMM YYYY
  -     HH:mm:ss,SSS" for example, "06 Nov 1994 08:49:37,459".
  -
  -     @param sbuf the string buffer to write to
  -  */
  -  public
  -  StringBuffer format(Date date, StringBuffer sbuf,
  -		      FieldPosition fieldPosition) {
  -
  -    calendar.setTime(date);
  -
  -    int day = calendar.get(Calendar.DAY_OF_MONTH);
  -    if(day < 10)
  -      sbuf.append('0');
  -    sbuf.append(day);
  -    sbuf.append(' ');
  -    sbuf.append(shortMonths[calendar.get(Calendar.MONTH)]);
  -    sbuf.append(' ');
  -
  -    int year =  calendar.get(Calendar.YEAR);
  -    sbuf.append(year);
  -    sbuf.append(' ');
  -
  -    return super.format(date, sbuf, fieldPosition);
  +   *  Constructs a DateTimeDateFormat using the default locale and time zone.
  +   */
  +  public DateTimeDateFormat() {
  +    super("dd MMM yyyy HH:mm:ss,SSS");
     }
   
     /**
  -     This method does not do anything but return <code>null</code>.
  +   *  Constructs a DateTimeDateFormat using the default locale and 
  +   *      a given time zone.
  +   *
  +   *  @param timeZone the given time zone.
      */
  -  public
  -  Date parse(java.lang.String s, ParsePosition pos) {
  -    return null;
  +  public DateTimeDateFormat(final TimeZone timeZone) {
  +    super("dd MMM yyyy HH:mm:ss,SSS");
  +    setTimeZone(timeZone);
     }
  +
   }
  
  
  
  1.7       +26 -113   logging-log4j/src/java/org/apache/log4j/helpers/ISO8601DateFormat.java
  
  Index: ISO8601DateFormat.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/ISO8601DateFormat.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ISO8601DateFormat.java	24 Apr 2002 01:16:13 -0000	1.6
  +++ ISO8601DateFormat.java	21 Dec 2004 04:19:47 -0000	1.7
  @@ -1,18 +1,23 @@
   /*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  - *
  - * 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 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.helpers;
   
  -import java.util.Calendar;
   import java.util.TimeZone;
  -import java.util.Date;
  -import java.text.FieldPosition;
  -import java.text.ParsePosition;
  +import java.text.SimpleDateFormat;
   
   // Contributors: Arndt Schoenewald <ar...@ibm23093i821.mc.schoenewald.de>
   
  @@ -30,116 +35,24 @@
   
      @since 0.7.5
   */
  -public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
  -
  -  public
  -  ISO8601DateFormat() {
  -  }
  -
  -  public
  -  ISO8601DateFormat(TimeZone timeZone) {
  -    super(timeZone);
  -  }
  -
  -  static private long   lastTime;
  -  static private char[] lastTimeString = new char[20];
  +public class ISO8601DateFormat extends SimpleDateFormat {
   
     /**
  -     Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
  -     to <code>sbuf</code>. For example: "1999-11-27 15:49:37,459".
  -
  -     @param sbuf the <code>StringBuffer</code> to write to
  -  */
  -  public
  -  StringBuffer format(Date date, StringBuffer sbuf,
  -		      FieldPosition fieldPosition) {
  -
  -    long now = date.getTime();
  -    int millis = (int)(now % 1000);
  -
  -    if ((now - millis) != lastTime) {
  -      // We reach this point at most once per second
  -      // across all threads instead of each time format()
  -      // is called. This saves considerable CPU time.
  -
  -      calendar.setTime(date);
  -
  -      int start = sbuf.length();
  -
  -      int year =  calendar.get(Calendar.YEAR);
  -      sbuf.append(year);
  -
  -      String month;
  -      switch(calendar.get(Calendar.MONTH)) {
  -      case Calendar.JANUARY: month = "-01-"; break;
  -      case Calendar.FEBRUARY: month = "-02-";  break;
  -      case Calendar.MARCH: month = "-03-"; break;
  -      case Calendar.APRIL: month = "-04-";  break;
  -      case Calendar.MAY: month = "-05-"; break;
  -      case Calendar.JUNE: month = "-06-";  break;
  -      case Calendar.JULY: month = "-07-"; break;
  -      case Calendar.AUGUST: month = "-08-";  break;
  -      case Calendar.SEPTEMBER: month = "-09-"; break;
  -      case Calendar.OCTOBER: month = "-10-"; break;
  -      case Calendar.NOVEMBER: month = "-11-";  break;
  -      case Calendar.DECEMBER: month = "-12-";  break;
  -      default: month = "-NA-"; break;
  -      }
  -      sbuf.append(month);
  -
  -      int day = calendar.get(Calendar.DAY_OF_MONTH);
  -      if(day < 10)
  -	sbuf.append('0');
  -      sbuf.append(day);
  -
  -      sbuf.append(' ');
  -
  -      int hour = calendar.get(Calendar.HOUR_OF_DAY);
  -      if(hour < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(hour);
  -      sbuf.append(':');
  -
  -      int mins = calendar.get(Calendar.MINUTE);
  -      if(mins < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(mins);
  -      sbuf.append(':');
  -
  -      int secs = calendar.get(Calendar.SECOND);
  -      if(secs < 10) {
  -	sbuf.append('0');
  -      }
  -      sbuf.append(secs);
  -
  -      sbuf.append(',');
  -
  -      // store the time string for next time to avoid recomputation
  -      sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
  -      lastTime = now - millis;
  -    }
  -    else {
  -      sbuf.append(lastTimeString);
  -    }
  -
  -
  -    if (millis < 100)
  -      sbuf.append('0');
  -    if (millis < 10)
  -      sbuf.append('0');
  -
  -    sbuf.append(millis);
  -    return sbuf;
  +   *  Constructs an ISOTimeDateFormat using the default locale and time zone.
  +   */
  +  public ISO8601DateFormat() {
  +     super("yyyy-MM-dd HH:mm:ss,SSS");
     }
   
     /**
  -    This method does not do anything but return <code>null</code>.
  +   *  Constructs an ISO8601DateFormat using the default locale and 
  +   *      a given time zone.
  +   *
  +   *  @param timeZone the given time zone.
      */
  -  public
  -  Date parse(java.lang.String s, ParsePosition pos) {
  -    return null;
  +  public ISO8601DateFormat(final TimeZone timeZone) {
  +     super("yyyy-MM-dd HH:mm:ss,SSS");
  +     setTimeZone(timeZone);
     }
   }
   
  
  
  
  1.4       +35 -5     logging-log4j/src/java/org/apache/log4j/helpers/RelativeTimeDateFormat.java
  
  Index: RelativeTimeDateFormat.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/RelativeTimeDateFormat.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RelativeTimeDateFormat.java	5 Jul 2001 19:09:45 -0000	1.3
  +++ RelativeTimeDateFormat.java	21 Dec 2004 04:19:47 -0000	1.4
  @@ -1,16 +1,27 @@
   /*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  - *
  - * 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 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.helpers;
   
   import java.util.Date;
  +import java.util.TimeZone;
   import java.text.FieldPosition;
   import java.text.ParsePosition;
   import java.text.DateFormat;
  +import java.text.NumberFormat;
   
   /**
      Formats a {@link Date} by printing the number of milliseconds
  @@ -50,4 +61,23 @@
     Date parse(java.lang.String s, ParsePosition pos) {
       return null;
     }  
  +  
  +
  +  /**
  +   * Gets the number format.
  +   * @return NumberFormat number format
  +   */
  +  public NumberFormat getNumberFormat() {
  +    return NumberFormat.getIntegerInstance();
  +  }
  +
  +  /**
  +   * Sets the timezone.
  +   * @remarks Ignored by this formatter, but intercepted to prevent
  +   * NullPointerException in superclass.
  +   * @param tz TimeZone timezone
  +   */
  +  public void setTimeZone(final TimeZone tz) {
  +  }
  +  
   }
  
  
  

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