View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.helpers;
19  
20  import org.apache.log4j.Layout;
21  import org.apache.log4j.spi.LoggingEvent;
22  import java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.TimeZone;
26  import java.text.FieldPosition;
27  
28  
29  /**
30     This abstract layout takes care of all the date related options and
31     formatting work.
32     
33  
34     @author Ceki Gülcü
35   */
36  abstract public class DateLayout extends Layout {
37  
38    /**
39       String constant designating no time information. Current value of
40       this constant is <b>NULL</b>.
41       
42    */
43    public final static String NULL_DATE_FORMAT = "NULL";
44  
45    /**
46       String constant designating relative time. Current value of
47       this constant is <b>RELATIVE</b>.
48     */
49    public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
50  
51    protected FieldPosition pos = new FieldPosition(0);
52  
53    /**
54       @deprecated Options are now handled using the JavaBeans paradigm.
55       This constant is not longer needed and will be removed in the
56       <em>near</em> term.
57    */
58    final static public String DATE_FORMAT_OPTION = "DateFormat";
59    
60    /**
61       @deprecated Options are now handled using the JavaBeans paradigm.
62       This constant is not longer needed and will be removed in the
63       <em>near</em> term.
64    */
65    final static public String TIMEZONE_OPTION = "TimeZone";  
66  
67    private String timeZoneID;
68    private String dateFormatOption;  
69  
70    protected DateFormat dateFormat;
71    protected Date date = new Date();
72  
73    /**
74       @deprecated Use the setter method for the option directly instead
75       of the generic <code>setOption</code> method. 
76    */
77    public
78    String[] getOptionStrings() {
79      return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
80    }
81  
82    /**
83       @deprecated Use the setter method for the option directly instead
84       of the generic <code>setOption</code> method. 
85    */
86    public
87    void setOption(String option, String value) {
88      if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) {
89        dateFormatOption = value.toUpperCase();
90      } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) {
91        timeZoneID = value;
92      }
93    }
94    
95  
96    /**
97      The value of the <b>DateFormat</b> option should be either an
98      argument to the constructor of {@link SimpleDateFormat} or one of
99      the srings "NULL", "RELATIVE", "ABSOLUTE", "DATE" or "ISO8601.
100    */
101   public
102   void setDateFormat(String dateFormat) {
103     if (dateFormat != null) {
104         dateFormatOption = dateFormat;
105     }
106     setDateFormat(dateFormatOption, TimeZone.getDefault());
107   }
108 
109   /**
110      Returns value of the <b>DateFormat</b> option.
111    */
112   public
113   String getDateFormat() {
114     return dateFormatOption;
115   }
116   
117   /**
118     The <b>TimeZoneID</b> option is a time zone ID string in the format
119     expected by the {@link TimeZone#getTimeZone} method.
120    */
121   public
122   void setTimeZone(String timeZone) {
123     this.timeZoneID = timeZone;
124   }
125   
126   /**
127      Returns value of the <b>TimeZone</b> option.
128    */
129   public
130   String getTimeZone() {
131     return timeZoneID;
132   }
133   
134   public
135   void activateOptions() {
136     setDateFormat(dateFormatOption);
137     if(timeZoneID != null && dateFormat != null) {
138       dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
139     }
140   }
141 
142   public
143   void dateFormat(StringBuffer buf, LoggingEvent event) {
144     if(dateFormat != null) {
145       date.setTime(event.timeStamp);
146       dateFormat.format(date, buf, this.pos);
147       buf.append(' ');
148     }
149   }
150 
151   /**
152      Sets the {@link DateFormat} used to format time and date in the
153      zone determined by <code>timeZone</code>.
154    */
155   public
156   void setDateFormat(DateFormat dateFormat, TimeZone timeZone) {
157     this.dateFormat = dateFormat;    
158     this.dateFormat.setTimeZone(timeZone);
159   }
160   
161   /**
162      Sets the DateFormat used to format date and time in the time zone
163      determined by <code>timeZone</code> parameter. The {@link DateFormat} used
164      will depend on the <code>dateFormatType</code>.
165 
166      <p>The recognized types are {@link #NULL_DATE_FORMAT}, {@link
167      #RELATIVE_TIME_DATE_FORMAT} {@link
168      AbsoluteTimeDateFormat#ABS_TIME_DATE_FORMAT}, {@link
169      AbsoluteTimeDateFormat#DATE_AND_TIME_DATE_FORMAT} and {@link
170      AbsoluteTimeDateFormat#ISO8601_DATE_FORMAT}. If the
171      <code>dateFormatType</code> is not one of the above, then the
172      argument is assumed to be a date pattern for {@link
173      SimpleDateFormat}.
174   */
175   public
176   void setDateFormat(String dateFormatType, TimeZone timeZone) {
177     if(dateFormatType == null) {
178       this.dateFormat = null;
179       return;
180     } 
181 
182     if(dateFormatType.equalsIgnoreCase(NULL_DATE_FORMAT)) {
183       this.dateFormat = null;
184     } else if (dateFormatType.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) {
185       this.dateFormat =  new RelativeTimeDateFormat();
186     } else if(dateFormatType.equalsIgnoreCase(
187                              AbsoluteTimeDateFormat.ABS_TIME_DATE_FORMAT)) {
188       this.dateFormat =  new AbsoluteTimeDateFormat(timeZone);
189     } else if(dateFormatType.equalsIgnoreCase(
190                         AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT)) {
191       this.dateFormat =  new DateTimeDateFormat(timeZone);
192     } else if(dateFormatType.equalsIgnoreCase(
193                               AbsoluteTimeDateFormat.ISO8601_DATE_FORMAT)) {
194       this.dateFormat =  new ISO8601DateFormat(timeZone);
195     } else {
196       this.dateFormat = new SimpleDateFormat(dateFormatType);
197       this.dateFormat.setTimeZone(timeZone);
198     }
199   }
200 }