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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.helpers;
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;
30 This abstract layout takes care of all the date related options and
34 @author Ceki Gülcü
36 abstract public class DateLayout extends Layout {
39 String constant designating no time information. Current value of
40 this constant is <b>NULL</b>.
43 public final static String NULL_DATE_FORMAT = "NULL";
46 String constant designating relative time. Current value of
47 this constant is <b>RELATIVE</b>.
49 public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
51 protected FieldPosition pos = new FieldPosition(0);
54 @deprecated Options are now handled using the JavaBeans paradigm.
55 This constant is not longer needed and will be removed in the
58 final static public String DATE_FORMAT_OPTION = "DateFormat";
61 @deprecated Options are now handled using the JavaBeans paradigm.
62 This constant is not longer needed and will be removed in the
65 final static public String TIMEZONE_OPTION = "TimeZone";
67 private String timeZoneID;
68 private String dateFormatOption;
70 protected DateFormat dateFormat;
71 protected Date date = new Date();
74 @deprecated Use the setter method for the option directly instead
75 of the generic <code>setOption</code> method.
78 String[] getOptionStrings() {
79 return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
83 @deprecated Use the setter method for the option directly instead
84 of the generic <code>setOption</code> method.
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)) {
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.
102 void setDateFormat(String dateFormat) {
103 if (dateFormat != null) {
104 dateFormatOption = dateFormat;
106 setDateFormat(dateFormatOption, TimeZone.getDefault());
110 Returns value of the <b>DateFormat</b> option.
113 String getDateFormat() {
114 return dateFormatOption;
118 The <b>TimeZoneID</b> option is a time zone ID string in the format
119 expected by the {@link TimeZone#getTimeZone} method.
122 void setTimeZone(String timeZone) {
123 this.timeZoneID = timeZone;
127 Returns value of the <b>TimeZone</b> option.
130 String getTimeZone() {
135 void activateOptions() {
136 setDateFormat(dateFormatOption);
137 if(timeZoneID != null && dateFormat != null) {
138 dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
143 void dateFormat(StringBuffer buf, LoggingEvent event) {
144 if(dateFormat != null) {
145 date.setTime(event.timeStamp);
146 dateFormat.format(date, buf, this.pos);
152 Sets the {@link DateFormat} used to format time and date in the
153 zone determined by <code>timeZone</code>.
156 void setDateFormat(DateFormat dateFormat, TimeZone timeZone) {
157 this.dateFormat = dateFormat;
158 this.dateFormat.setTimeZone(timeZone);
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>.
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
176 void setDateFormat(String dateFormatType, TimeZone timeZone) {
177 if(dateFormatType == null) {
178 this.dateFormat = null;
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);
196 this.dateFormat = new SimpleDateFormat(dateFormatType);
197 this.dateFormat.setTimeZone(timeZone);