View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
3    * reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
6    * v1.0 as published by the Eclipse Foundation
7    *
8    * or (per the licensee's choosing)
9    *
10   * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
11   */
12  package ch.qos.logback.core.rolling;
13  
14  import java.io.File;
15  import java.time.Instant;
16  import java.util.Date;
17  
18  import ch.qos.logback.core.joran.spi.NoAutoStart;
19  import ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover;
20  
21  /**
22   * Default implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}
23   * interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested
24   * within a {@link TimeBasedRollingPolicy}.
25   *
26   *
27   * @author Ceki Gülcü
28   *
29   * @param <E>
30   */
31  @NoAutoStart
32  public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
33  
34      @Override
35      public void start() {
36          super.start();
37          if (!super.isErrorFree())
38              return;
39          if (tbrp.fileNamePattern.hasIntegerTokenCOnverter()) {
40              addError("Filename pattern [" + tbrp.fileNamePattern
41                      + "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Remove it.");
42              return;
43          }
44  
45          archiveRemover = new TimeBasedArchiveRemover(tbrp.fileNamePattern, rc);
46          archiveRemover.setContext(context);
47          started = true;
48      }
49  
50      public boolean isTriggeringEvent(File activeFile, final E event) {
51          long currentTime = getCurrentTime();
52          long localNextCheck = atomicNextCheck.get();
53          if (currentTime >= localNextCheck) {
54              long nextCheck = computeNextCheck(currentTime);
55              atomicNextCheck.set(nextCheck);
56              Instant instantOfElapsedPeriod = dateInCurrentPeriod;
57              addInfo("Elapsed period: " + instantOfElapsedPeriod.toString());
58              this.elapsedPeriodsFileName = tbrp.fileNamePatternWithoutCompSuffix.convert(instantOfElapsedPeriod);
59              setDateInCurrentPeriod(currentTime);
60              return true;
61          } else {
62              return false;
63          }
64      }
65  
66      @Override
67      public String toString() {
68          return "c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy";
69      }
70  
71  }