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.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          // if the option list contains a TZ option, then set it.
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              // if zoneId is null, the CachingDateFormatter will use the ZoneId.systemDefault()
58              // if locale is null, the CachingDateFormatter will use the Locale.getDefault()
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       * This method is intended for test classes. Should not be used
78       * by regular clients.
79       *
80       * @return the CachingDateFormatter in use
81       * @deprecated Will be removed in future versions with no replacement
82       */
83      @Deprecated
84      public CachingDateFormatter internalCachingDateFormatter() {
85          return cachingDateFormatter;
86      }
87  }