1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
35
36 abstract public class DateLayout extends Layout {
37
38
39
40
41
42
43 public final static String NULL_DATE_FORMAT = "NULL";
44
45
46
47
48
49 public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
50
51 protected FieldPosition pos = new FieldPosition(0);
52
53
54
55
56
57
58 final static public String DATE_FORMAT_OPTION = "DateFormat";
59
60
61
62
63
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
75
76
77 public
78 String[] getOptionStrings() {
79 return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
80 }
81
82
83
84
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
98
99
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
111
112 public
113 String getDateFormat() {
114 return dateFormatOption;
115 }
116
117
118
119
120
121 public
122 void setTimeZone(String timeZone) {
123 this.timeZoneID = timeZone;
124 }
125
126
127
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
153
154
155 public
156 void setDateFormat(DateFormat dateFormat, TimeZone timeZone) {
157 this.dateFormat = dateFormat;
158 this.dateFormat.setTimeZone(timeZone);
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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 }