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;
15  
16  import static ch.qos.logback.core.CoreConstants.CODES_URL;
17  
18  import java.io.File;
19  import java.util.Date;
20  import java.util.Locale;
21  
22  import ch.qos.logback.core.CoreConstants;
23  import ch.qos.logback.core.rolling.helper.ArchiveRemover;
24  import ch.qos.logback.core.rolling.helper.DateTokenConverter;
25  import ch.qos.logback.core.rolling.helper.RollingCalendar;
26  import ch.qos.logback.core.spi.ContextAwareBase;
27  
28  abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends ContextAwareBase implements TimeBasedFileNamingAndTriggeringPolicy<E> {
29  
30      static private String COLLIDING_DATE_FORMAT_URL = CODES_URL + "#rfa_collision_in_dateFormat";
31  
32      protected TimeBasedRollingPolicy<E> tbrp;
33  
34      protected ArchiveRemover archiveRemover = null;
35      protected String elapsedPeriodsFileName;
36      protected RollingCalendar rc;
37  
38      protected long artificialCurrentTime = -1;
39      protected Date dateInCurrentPeriod = null;
40  
41      protected long nextCheck;
42      protected boolean started = false;
43      protected boolean errorFree = true;
44  
45      public boolean isStarted() {
46          return started;
47      }
48  
49      public void start() {
50          DateTokenConverter<Object> dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter();
51          if (dtc == null) {
52              throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken");
53          }
54  
55          if (dtc.getTimeZone() != null) {
56              rc = new RollingCalendar(dtc.getDatePattern(), dtc.getTimeZone(), Locale.getDefault());
57          } else {
58              rc = new RollingCalendar(dtc.getDatePattern());
59          }
60          addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '" + tbrp.fileNamePattern.getPattern() + "'.");
61          rc.printPeriodicity(this);
62  
63          if (!rc.isCollisionFree()) {
64              addError("The date format in FileNamePattern will result in collisions in the names of archived log files.");
65              addError(CoreConstants.MORE_INFO_PREFIX + COLLIDING_DATE_FORMAT_URL);
66              withErrors();
67              return;
68          }
69  
70          setDateInCurrentPeriod(new Date(getCurrentTime()));
71          if (tbrp.getParentsRawFileProperty() != null) {
72              File currentFile = new File(tbrp.getParentsRawFileProperty());
73              if (currentFile.exists() && currentFile.canRead()) {
74                  setDateInCurrentPeriod(new Date(currentFile.lastModified()));
75              }
76          }
77          addInfo("Setting initial period to " + dateInCurrentPeriod);
78          computeNextCheck();
79      }
80  
81      public void stop() {
82          started = false;
83      }
84  
85      protected void computeNextCheck() {
86          nextCheck = rc.getNextTriggeringDate(dateInCurrentPeriod).getTime();
87      }
88  
89      protected void setDateInCurrentPeriod(long now) {
90          dateInCurrentPeriod.setTime(now);
91      }
92  
93      // allow Test classes to act on the dateInCurrentPeriod field to simulate old
94      // log files needing rollover
95      public void setDateInCurrentPeriod(Date _dateInCurrentPeriod) {
96          this.dateInCurrentPeriod = _dateInCurrentPeriod;
97      }
98  
99      public String getElapsedPeriodsFileName() {
100         return elapsedPeriodsFileName;
101     }
102 
103     public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
104         return tbrp.fileNamePatternWithoutCompSuffix.convert(dateInCurrentPeriod);
105     }
106 
107     public void setCurrentTime(long timeInMillis) {
108         artificialCurrentTime = timeInMillis;
109     }
110 
111     public long getCurrentTime() {
112         // if time is forced return the time set by user
113         if (artificialCurrentTime >= 0) {
114             return artificialCurrentTime;
115         } else {
116             return System.currentTimeMillis();
117         }
118     }
119 
120     public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
121         this.tbrp = _tbrp;
122 
123     }
124 
125     public ArchiveRemover getArchiveRemover() {
126         return archiveRemover;
127     }
128 
129     protected void withErrors() {
130         errorFree = false;
131     }
132     
133     protected boolean isErrorFree() {
134         return errorFree;
135     }
136 
137 }