View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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  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  
39          try {
40              cachingDateFormatter = new CachingDateFormatter(datePattern);
41              // maximumCacheValidity = CachedDateFormat.getMaximumCacheValidity(pattern);
42          } catch (IllegalArgumentException e) {
43              addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
44              addWarn("Defaulting to  " + CoreConstants.CLF_DATE_PATTERN);
45              cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN);
46          }
47  
48          List<String> optionList = getOptionList();
49  
50          // if the option list contains a TZ option, then set it.
51          if (optionList != null && optionList.size() > 1) {
52              TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
53              cachingDateFormatter.setTimeZone(tz);
54          }
55      }
56  
57      @Override
58      public String convert(IAccessEvent accessEvent) {
59          long timestamp = accessEvent.getTimeStamp();
60          return cachingDateFormatter.format(timestamp);
61      }
62  }