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     */
017    package org.apache.logging.log4j.core.appender.rolling;
018    
019    import org.apache.logging.log4j.core.LogEvent;
020    import org.apache.logging.log4j.core.config.plugins.Plugin;
021    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
022    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
023    import org.apache.logging.log4j.core.util.Integers;
024    
025    /**
026     * Triggering Policy that causes a rollover based on time.
027     */
028    @Plugin(name = "TimeBasedTriggeringPolicy", category = "Core", printObject = true)
029    public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
030    
031        private long nextRollover;
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        /**
043         * Initialize the policy.
044         * @param manager The RollingFileManager.
045         */
046        @Override
047        public void initialize(final RollingFileManager manager) {
048            this.manager = manager;
049            
050            // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
051            manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
052            
053            nextRollover = manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
054        }
055    
056        /**
057         * Determine whether a rollover should occur.
058         * @param event   A reference to the currently event.
059         * @return true if a rollover should occur.
060         */
061        @Override
062        public boolean isTriggeringEvent(final LogEvent event) {
063            if (manager.getFileSize() == 0) {
064                return false;
065            }
066            final long now = event.getTimeMillis();
067            if (now > nextRollover) {
068                nextRollover = manager.getPatternProcessor().getNextTime(now, interval, modulate);
069                return true;
070            }
071            return false;
072        }
073    
074        @Override
075        public String toString() {
076            return "TimeBasedTriggeringPolicy";
077        }
078    
079        /**
080         * Create a TimeBasedTriggeringPolicy.
081         * @param interval The interval between rollovers.
082         * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
083         * @return a TimeBasedTriggeringPolicy.
084         */
085        @PluginFactory
086        public static TimeBasedTriggeringPolicy createPolicy(
087                @PluginAttribute("interval") final String interval,
088                @PluginAttribute("modulate") final String modulate) {
089            final int increment = Integers.parseInt(interval, 1);
090            final boolean mod = Boolean.parseBoolean(modulate);
091            return new TimeBasedTriggeringPolicy(increment, mod);
092        }
093    }