001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling;
018
019import org.apache.logging.log4j.core.LogEvent;
020import org.apache.logging.log4j.core.config.plugins.Plugin;
021import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
022import org.apache.logging.log4j.core.config.plugins.PluginFactory;
023import org.apache.logging.log4j.core.util.Integers;
024
025/**
026 * Rolls a file over based on time.
027 */
028@Plugin(name = "TimeBasedTriggeringPolicy", category = "Core", printObject = true)
029public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
030
031    private long nextRolloverMillis;
032    private final int interval;
033    private final boolean modulate;
034
035    private RollingFileManager manager;
036
037    private TimeBasedTriggeringPolicy(final int interval, final boolean modulate) {
038        this.interval = interval;
039        this.modulate = modulate;
040    }
041
042    public int getInterval() {
043        return interval;
044    }
045
046    public long getNextRolloverMillis() {
047        return nextRolloverMillis;
048    }
049
050    /**
051     * Initializes the policy.
052     * @param manager The RollingFileManager.
053     */
054    @Override
055    public void initialize(final RollingFileManager manager) {
056        this.manager = manager;
057        
058        // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
059        manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
060        
061        nextRolloverMillis = manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
062    }
063
064    /**
065     * Determines whether a rollover should occur.
066     * @param event   A reference to the currently event.
067     * @return true if a rollover should occur.
068     */
069    @Override
070    public boolean isTriggeringEvent(final LogEvent event) {
071        if (manager.getFileSize() == 0) {
072            return false;
073        }
074        final long nowMillis = event.getTimeMillis();
075        if (nowMillis > nextRolloverMillis) {
076            nextRolloverMillis = manager.getPatternProcessor().getNextTime(nowMillis, interval, modulate);
077            return true;
078        }
079        return false;
080    }
081
082    /**
083     * Creates a TimeBasedTriggeringPolicy.
084     * @param interval The interval between rollovers.
085     * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
086     * @return a TimeBasedTriggeringPolicy.
087     */
088    @PluginFactory
089    public static TimeBasedTriggeringPolicy createPolicy(
090            @PluginAttribute("interval") final String interval,
091            @PluginAttribute("modulate") final String modulate) {
092        final int increment = Integers.parseInt(interval, 1);
093        final boolean mod = Boolean.parseBoolean(modulate);
094        return new TimeBasedTriggeringPolicy(increment, mod);
095    }
096
097    @Override
098    public String toString() {
099        return "TimeBasedTriggeringPolicy(nextRolloverMillis=" + nextRolloverMillis + ", interval=" + interval
100                + ", modulate=" + modulate + ")";
101    }
102
103}