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.pattern;
20 import org.apache.log4j.helpers.LogLog;
21 import org.apache.log4j.spi.LoggingEvent;
23 import java.text.SimpleDateFormat;
24 import java.text.DateFormat;
25 import java.text.FieldPosition;
26 import java.text.ParsePosition;
27 import java.util.Date;
28 import java.util.TimeZone;
32 * Convert and format the event's date in a StringBuffer.
34 * @author Ceki Gülcü
36 public final class DatePatternConverter extends LoggingEventPatternConverter {
38 * ABSOLUTE string literal.
40 private static final String ABSOLUTE_FORMAT = "ABSOLUTE";
42 * SimpleTimePattern for ABSOLUTE.
44 private static final String ABSOLUTE_TIME_PATTERN = "HH:mm:ss,SSS";
48 * DATE string literal.
50 private static final String DATE_AND_TIME_FORMAT = "DATE";
52 * SimpleTimePattern for DATE.
54 private static final String DATE_AND_TIME_PATTERN = "dd MMM yyyy HH:mm:ss,SSS";
57 * ISO8601 string literal.
59 private static final String ISO8601_FORMAT = "ISO8601";
61 * SimpleTimePattern for ISO8601.
63 private static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
67 private final CachedDateFormat df;
70 * This class wraps a DateFormat and forces the time zone to the
71 * default time zone before each format and parse request.
73 private static class DefaultZoneDateFormat extends DateFormat {
75 * Serialization version ID.
77 private static final long serialVersionUID = 1;
79 * Wrapped instance of DateFormat.
81 private final DateFormat dateFormat;
84 * Construct new instance.
85 * @param format format, may not be null.
87 public DefaultZoneDateFormat(final DateFormat format) {
94 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
95 dateFormat.setTimeZone(TimeZone.getDefault());
96 return dateFormat.format(date, toAppendTo, fieldPosition);
102 public Date parse(String source, ParsePosition pos) {
103 dateFormat.setTimeZone(TimeZone.getDefault());
104 return dateFormat.parse(source, pos);
109 * Private constructor.
110 * @param options options, may be null.
112 private DatePatternConverter(final String[] options) {
113 super("Date", "date");
115 String patternOption;
117 if ((options == null) || (options.length == 0)) {
118 // the branch could be optimized, but here we are making explicit
119 // that null values for patternOption are allowed.
120 patternOption = null;
122 patternOption = options[0];
128 (patternOption == null)
129 || patternOption.equalsIgnoreCase(ISO8601_FORMAT)) {
130 pattern = ISO8601_PATTERN;
131 } else if (patternOption.equalsIgnoreCase(ABSOLUTE_FORMAT)) {
132 pattern = ABSOLUTE_TIME_PATTERN;
133 } else if (patternOption.equalsIgnoreCase(DATE_AND_TIME_FORMAT)) {
134 pattern = DATE_AND_TIME_PATTERN;
136 pattern = patternOption;
139 int maximumCacheValidity = 1000;
140 DateFormat simpleFormat = null;
143 simpleFormat = new SimpleDateFormat(pattern);
144 maximumCacheValidity = CachedDateFormat.getMaximumCacheValidity(pattern);
145 } catch (IllegalArgumentException e) {
147 "Could not instantiate SimpleDateFormat with pattern "
150 // default to the ISO8601 format
151 simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
154 // if the option list contains a TZ option, then set it.
155 if ((options != null) && (options.length > 1)) {
156 TimeZone tz = TimeZone.getTimeZone(options[1]);
157 simpleFormat.setTimeZone(tz);
159 simpleFormat = new DefaultZoneDateFormat(simpleFormat);
162 df = new CachedDateFormat(simpleFormat, maximumCacheValidity);
166 * Obtains an instance of pattern converter.
167 * @param options options, may be null.
168 * @return instance of pattern converter.
170 public static DatePatternConverter newInstance(
171 final String[] options) {
172 return new DatePatternConverter(options);
178 public void format(final LoggingEvent event, final StringBuffer output) {
180 df.format(event.timeStamp, output);
187 public void format(final Object obj, final StringBuffer output) {
188 if (obj instanceof Date) {
189 format((Date) obj, output);
192 super.format(obj, output);
196 * Append formatted date to string buffer.
198 * @param toAppendTo buffer to which formatted date is appended.
200 public void format(final Date date, final StringBuffer toAppendTo) {
202 df.format(date.getTime(), toAppendTo);