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