package ch.qos.logback.core.rolling; import ch.qos.logback.core.joran.spi.NoAutoStart; import ch.qos.logback.core.rolling.helper.DateTokenConverter; import ch.qos.logback.core.rolling.helper.DefaultArchiveRemover; import ch.qos.logback.core.rolling.helper.PeriodicRollingCalendar; import java.io.File; import java.util.Date; /** * The default * {@link ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy} * is not capable of triggering rolling with intervals lie 10 mins or 5 hours or 2 days etc. * It can only trigger every 1 minute or 1 hour , 1 day. * The class TimeBasedFileNamingAndTriggeringPolicyBase extends * {@link ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicyBase} * to provide this functionality using a {@link ch.qos.logback.core.rolling.helper.PeriodRollingCalendar} * * @author rathmm * @param */ @NoAutoStart public class PeriodicTimeBasedFileNamingAndTriggeringPolicy extends TimeBasedFileNamingAndTriggeringPolicyBase { int period; public int getPeriod() { return period; } public void setPeriod(int period) { this.period = period; } @Override public void start() { /*Start of code copy from ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicyBase **/ /*Replaced RollingCalendar with PeriodicRollingCalendar*/ DateTokenConverter dtc = tbrp.fileNamePattern.getDateTokenConverter(); if (dtc == null) { throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken"); } rc = new PeriodicRollingCalendar(period); rc.init(dtc.getDatePattern()); addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '" + tbrp.fileNamePattern.getPattern() + "'."); rc.printPeriodicity(this); setDateInCurrentPeriod(new Date(getCurrentTime())); if (tbrp.getParentsRawFileProperty() != null) { File currentFile = new File(tbrp.getParentsRawFileProperty()); if (currentFile.exists() && currentFile.canRead()) { setDateInCurrentPeriod(new Date(currentFile.lastModified())); } } addInfo("Setting initial period to " + dateInCurrentPeriod); computeNextCheck(); /**End of code copy from ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicyBase **/ /**Start of code copy from ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy **/ archiveRemover = new DefaultArchiveRemover(tbrp.fileNamePattern, rc); archiveRemover.setContext(context); started = true; /**End of code copy from ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy **/ } @Override public boolean isTriggeringEvent(File activeFile, final E event) { long time = getCurrentTime(); if (time >= nextCheck) { Date dateOfElapsedPeriod = dateInCurrentPeriod; elapsedPeriodsFileName = tbrp.fileNamePatternWCS.convert(dateOfElapsedPeriod); setDateInCurrentPeriod(time); computeNextCheck(); return true; } else { return false; } } @Override public String toString() { return "c.q.l.core.rolling.PeriodicTimeBasedFileNamingAndTriggeringPolicy"; } }