1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import org.apache.log4j.helpers.LogLog;
21 import org.apache.log4j.spi.LoggingEvent;
22
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.TimeZone;
26
27
28 /***
29 * Convert and format the event's date in a StringBuffer.
30 *
31 * @author Ceki Gülcü
32 */
33 public final class DatePatternConverter extends LoggingEventPatternConverter {
34 /***
35 * ABSOLUTE string literal.
36 */
37 private static final String ABSOLUTE_FORMAT = "ABSOLUTE";
38 /***
39 * SimpleTimePattern for ABSOLUTE.
40 */
41 private static final String ABSOLUTE_TIME_PATTERN = "HH:mm:ss,SSS";
42
43
44 /***
45 * DATE string literal.
46 */
47 private static final String DATE_AND_TIME_FORMAT = "DATE";
48 /***
49 * SimpleTimePattern for DATE.
50 */
51 private static final String DATE_AND_TIME_PATTERN = "dd MMM yyyy HH:mm:ss,SSS";
52
53 /***
54 * ISO8601 string literal.
55 */
56 private static final String ISO8601_FORMAT = "ISO8601";
57 /***
58 * SimpleTimePattern for ISO8601.
59 */
60 private static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
61 /***
62 * Date format.
63 */
64 private final CachedDateFormat df;
65
66 /***
67 * Private constructor.
68 * @param options options, may be null.
69 */
70 private DatePatternConverter(final String[] options) {
71 super("Date", "date");
72
73 String patternOption;
74
75 if ((options == null) || (options.length == 0)) {
76
77
78 patternOption = null;
79 } else {
80 patternOption = options[0];
81 }
82
83 String pattern;
84
85 if (
86 (patternOption == null)
87 || patternOption.equalsIgnoreCase(ISO8601_FORMAT)) {
88 pattern = ISO8601_PATTERN;
89 } else if (patternOption.equalsIgnoreCase(ABSOLUTE_FORMAT)) {
90 pattern = ABSOLUTE_TIME_PATTERN;
91 } else if (patternOption.equalsIgnoreCase(DATE_AND_TIME_FORMAT)) {
92 pattern = DATE_AND_TIME_PATTERN;
93 } else {
94 pattern = patternOption;
95 }
96
97 int maximumCacheValidity = 1000;
98 SimpleDateFormat simpleFormat = null;
99
100 try {
101 simpleFormat = new SimpleDateFormat(pattern);
102 maximumCacheValidity = CachedDateFormat.getMaximumCacheValidity(pattern);
103 } catch (IllegalArgumentException e) {
104 LogLog.warn(
105 "Could not instantiate SimpleDateFormat with pattern "
106 + patternOption, e);
107
108
109 simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
110 }
111
112
113 if ((options != null) && (options.length > 1)) {
114 TimeZone tz = TimeZone.getTimeZone((String) options[1]);
115 simpleFormat.setTimeZone(tz);
116 }
117
118 df = new CachedDateFormat(simpleFormat, maximumCacheValidity);
119 }
120
121 /***
122 * Obtains an instance of pattern converter.
123 * @param options options, may be null.
124 * @return instance of pattern converter.
125 */
126 public static DatePatternConverter newInstance(
127 final String[] options) {
128 return new DatePatternConverter(options);
129 }
130
131 /***
132 * {@inheritDoc}
133 */
134 public void format(final LoggingEvent event, final StringBuffer output) {
135 synchronized(this) {
136 df.format(event.timeStamp, output);
137 }
138 }
139
140 /***
141 * {@inheritDoc}
142 */
143 public void format(final Object obj, final StringBuffer output) {
144 if (obj instanceof Date) {
145 format((Date) obj, output);
146 }
147
148 super.format(obj, output);
149 }
150
151 /***
152 * Append formatted date to string buffer.
153 * @param date date
154 * @param toAppendTo buffer to which formatted date is appended.
155 */
156 public void format(final Date date, final StringBuffer toAppendTo) {
157 synchronized(this) {
158 df.format(date.getTime(), toAppendTo);
159 }
160 }
161 }