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