001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights 003 * reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License 006 * v1.0 as published by the Eclipse Foundation 007 * 008 * or (per the licensee's choosing) 009 * 010 * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. 011 */ 012package ch.qos.logback.core.rolling; 013 014import java.io.File; 015import java.time.Instant; 016import java.time.ZoneId; 017import java.time.ZonedDateTime; 018import java.util.Date; 019 020import ch.qos.logback.core.joran.spi.NoAutoStart; 021import ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover; 022 023/** 024 * Default implementation of {@link TimeBasedFileNamingAndTriggeringPolicy} 025 * interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested 026 * within a {@link TimeBasedRollingPolicy}. 027 * 028 * 029 * @author Ceki Gülcü 030 * 031 * @param <E> 032 */ 033@NoAutoStart 034public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> { 035 036 @Override 037 public void start() { 038 super.start(); 039 if (!super.isErrorFree()) 040 return; 041 if (enclosingTBRP.fileNamePattern.hasIntegerTokenCOnverter()) { 042 addError("Filename pattern [" + enclosingTBRP.fileNamePattern 043 + "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Please remove it."); 044 return; 045 } 046 047 archiveRemover = new TimeBasedArchiveRemover(enclosingTBRP.fileNamePattern, 048 enclosingTBRP.fileNamePatternWithoutCompSuffix, rc); 049 archiveRemover.setContext(context); 050 started = true; 051 } 052 053 public boolean isTriggeringEvent(File activeFile, final E event) { 054 long currentTime = getCurrentTime(); 055 long localNextCheck = atomicNextCheck.get(); 056 if (currentTime >= localNextCheck) { 057 long nextCheck = computeNextCheck(currentTime); 058 atomicNextCheck.set(nextCheck); 059 Instant instantOfElapsedPeriod = dateInCurrentPeriod; 060 ZonedDateTime ztd = instantOfElapsedPeriod.atZone(zoneId); 061 addInfo("Elapsed period: " + ztd.toString()); 062 this.elapsedPeriodsFileName = enclosingTBRP.fileNamePatternWithoutCompSuffix.convert(instantOfElapsedPeriod); 063 setDateInCurrentPeriod(currentTime); 064 return true; 065 } else { 066 return false; 067 } 068 } 069 070 @Override 071 public String toString() { 072 return "c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy"; 073 } 074 075}