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.core.rolling.helper;
15  
16  import java.time.Instant;
17  import java.time.ZoneId;
18  import java.util.Date;
19  import java.util.List;
20  
21  import ch.qos.logback.core.CoreConstants;
22  import ch.qos.logback.core.pattern.DynamicConverter;
23  import ch.qos.logback.core.util.CachingDateFormatter;
24  import ch.qos.logback.core.util.DatePatternToRegexUtil;
25  
26  /**
27   * Returns a date formatted by SimpleDateFormatter.
28   * 
29   * @author Ceki Gülcü
30   */
31  public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTypedConverter {
32  
33      /**
34       * The conversion word/character with which this converter is registered.
35       */
36      public final static String CONVERTER_KEY = "d";
37      public final static String AUXILIARY_TOKEN = "AUX";
38      public static final String DEFAULT_DATE_PATTERN = CoreConstants.DAILY_DATE_PATTERN;
39  
40      private String datePattern;
41      private ZoneId zoneId;
42      private CachingDateFormatter cdf;
43      // is this token converter primary or auxiliary? Only the primary converter
44      // determines the rolling period
45      private boolean primary = true;
46  
47      public void start() {
48          this.datePattern = getFirstOption();
49          if (this.datePattern == null) {
50              this.datePattern = DEFAULT_DATE_PATTERN;
51          }
52  
53          final List<String> optionList = getOptionList();
54          if (optionList != null) {
55              for (int optionIndex = 1; optionIndex < optionList.size(); optionIndex++) {
56                  String option = optionList.get(optionIndex);
57                  if (AUXILIARY_TOKEN.equalsIgnoreCase(option)) {
58                      primary = false;
59                  } else {
60                      zoneId = ZoneId.of(option);
61                  }
62              }
63          }
64  
65          cdf = new CachingDateFormatter(datePattern, zoneId);
66      }
67  
68      public String convert(Date date) {
69          return cdf.format(date.getTime());
70      }
71  
72      public String convert(Instant instant) {
73          return cdf.format(instant.toEpochMilli());
74      }
75  
76      public String convert(Object o) {
77          if (o == null) {
78              throw new IllegalArgumentException("Null argument forbidden");
79          }
80          if (o instanceof Date) {
81              return convert((Date) o);
82          }
83          if (o instanceof Instant) {
84              return convert((Instant) o);
85          }
86  
87          throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName());
88      }
89  
90      /**
91       * Return the date pattern.
92       */
93      public String getDatePattern() {
94          return datePattern;
95      }
96  
97      public ZoneId getZoneId() {
98          return zoneId;
99      }
100 
101     public boolean isApplicable(Object o) {
102         if(o instanceof Date)
103             return true;
104         if(o instanceof Instant)
105             return true;
106         return false;
107     }
108 
109     public String toRegex() {
110         DatePatternToRegexUtil datePatternToRegexUtil = new DatePatternToRegexUtil(datePattern);
111         return datePatternToRegexUtil.toRegex();
112     }
113 
114     public boolean isPrimary() {
115         return primary;
116     }
117 }