1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.common.pattern;
15
16 import java.time.ZoneId;
17 import java.util.List;
18 import java.util.Locale;
19
20 import ch.qos.logback.access.common.spi.IAccessEvent;
21 import ch.qos.logback.core.CoreConstants;
22 import ch.qos.logback.core.util.CachingDateFormatter;
23
24 public class DateConverter extends AccessConverter {
25
26
27 CachingDateFormatter cachingDateFormatter = null;
28
29 @Override
30 public void start() {
31
32 String datePattern = getFirstOption();
33
34 if (datePattern == null) {
35 datePattern = CoreConstants.CLF_DATE_PATTERN;
36 } else if (datePattern.equals(CoreConstants.ISO8601_STR)) {
37 datePattern = CoreConstants.ISO8601_PATTERN;
38 } else if (datePattern.equals(CoreConstants.STRICT_STR)) {
39 datePattern = CoreConstants.STRICT_ISO8601_PATTERN;
40 }
41
42 List<String> optionList = getOptionList();
43 ZoneId zoneId = null;
44
45 if (optionList != null && optionList.size() > 1) {
46 String zoneIdString = (String) optionList.get(1);
47 zoneId = ZoneId.of(zoneIdString);
48 }
49 Locale locale = null;
50 if (optionList != null && optionList.size() > 2) {
51 String localeIdStr = (String) optionList.get(2);
52 locale = Locale.forLanguageTag(localeIdStr);
53 addInfo("Setting locale to \""+locale+"\"");
54 }
55
56 try {
57
58
59 cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId, locale);
60 } catch (IllegalArgumentException e) {
61 addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
62 addWarn("Defaulting to " + CoreConstants.CLF_DATE_PATTERN);
63 cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN, zoneId);
64 }
65
66 super.start();
67 }
68
69 @Override
70
71 public String convert(IAccessEvent accessEvent) {
72 long timestamp = accessEvent.getTimeStamp();
73 return cachingDateFormatter.format(timestamp);
74 }
75
76
77
78
79
80
81
82
83 @Deprecated
84 public CachingDateFormatter internalCachingDateFormatter() {
85 return cachingDateFormatter;
86 }
87 }