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