1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.pattern;
15
16 import java.util.List;
17 import java.util.TimeZone;
18
19 import ch.qos.logback.access.spi.IAccessEvent;
20 import ch.qos.logback.core.CoreConstants;
21 import ch.qos.logback.core.util.CachingDateFormatter;
22
23
24 public class DateConverter extends AccessConverter {
25
26
27 CachingDateFormatter cachingDateFormatter = null;
28
29 public void start() {
30
31 String datePattern = getFirstOption();
32 if(datePattern == null) {
33 datePattern = CoreConstants.CLF_DATE_PATTERN;
34 }
35
36 if (datePattern.equals(CoreConstants.ISO8601_STR)) {
37 datePattern = CoreConstants.ISO8601_PATTERN;
38 }
39
40 try {
41 cachingDateFormatter = new CachingDateFormatter(datePattern);
42
43 } catch (IllegalArgumentException e) {
44 addWarn(
45 "Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
46
47 cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN);
48 }
49
50 List optionList = getOptionList();
51
52
53 if (optionList != null && optionList.size() > 1) {
54 TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
55 cachingDateFormatter.setTimeZone(tz);
56 }
57 }
58
59
60 public String convert(IAccessEvent accessEvent) {
61 long timestamp = accessEvent.getTimeStamp();
62 return cachingDateFormatter.format(timestamp);
63 }
64 }