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.time.ZoneId;
17  import java.time.ZonedDateTime;
18  import java.util.Date;
19  
20  import ch.qos.logback.core.joran.spi.NoAutoStart;
21  import ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover;
22  
23  /**
24   * Default implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}
25   * interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested
26   * within a {@link TimeBasedRollingPolicy}.
27   *
28   *
29   * @author Ceki Gülcü
30   *
31   * @param <E>
32   */
33  @NoAutoStart
34  public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
35  
36      @Override
37      public void start() {
38          super.start();
39          if (!super.isErrorFree())
40              return;
41          if (tbrp.fileNamePattern.hasIntegerTokenCOnverter()) {
42              addError("Filename pattern [" + tbrp.fileNamePattern
43                      + "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Please remove it.");
44              return;
45          }
46  
47          archiveRemover = new TimeBasedArchiveRemover(tbrp.fileNamePattern, rc);
48          archiveRemover.setContext(context);
49          started = true;
50      }
51  
52      public boolean isTriggeringEvent(File activeFile, final E event) {
53          long currentTime = getCurrentTime();
54          long localNextCheck = atomicNextCheck.get();
55          if (currentTime >= localNextCheck) {
56              long nextCheck = computeNextCheck(currentTime);
57              atomicNextCheck.set(nextCheck);
58              Instant instantOfElapsedPeriod = dateInCurrentPeriod;
59              ZonedDateTime ztd = instantOfElapsedPeriod.atZone(zoneId);
60              addInfo("Elapsed period: " + ztd.toString());
61              this.elapsedPeriodsFileName = tbrp.fileNamePatternWithoutCompSuffix.convert(instantOfElapsedPeriod);
62              setDateInCurrentPeriod(currentTime);
63              return true;
64          } else {
65              return false;
66          }
67      }
68  
69      @Override
70      public String toString() {
71          return "c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy";
72      }
73  
74  }