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 (tbrp.fileNamePattern.hasIntegerTokenCOnverter()) {
042            addError("Filename pattern [" + tbrp.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(tbrp.fileNamePattern, rc);
048        archiveRemover.setContext(context);
049        started = true;
050    }
051
052    public boolean isTriggeringEvent(File activeFile, final E event) {
053        long currentTime = getCurrentTime();
054        long localNextCheck = atomicNextCheck.get();
055        if (currentTime >= localNextCheck) {
056            long nextCheck = computeNextCheck(currentTime);
057            atomicNextCheck.set(nextCheck);
058            Instant instantOfElapsedPeriod = dateInCurrentPeriod;
059            ZonedDateTime ztd = instantOfElapsedPeriod.atZone(zoneId);
060            addInfo("Elapsed period: " + ztd.toString());
061            this.elapsedPeriodsFileName = tbrp.fileNamePatternWithoutCompSuffix.convert(instantOfElapsedPeriod);
062            setDateInCurrentPeriod(currentTime);
063            return true;
064        } else {
065            return false;
066        }
067    }
068
069    @Override
070    public String toString() {
071        return "c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy";
072    }
073
074}