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.PluginAttr;
022    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
023    
024    /**
025     * Triggering Policy that causes a rollover based on time.
026     */
027    @Plugin(name = "TimeBasedTriggeringPolicy", category = "Core", printObject = true)
028    public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
029    
030        private long nextRollover;
031        private final int interval;
032        private final boolean modulate;
033    
034        private RollingFileManager manager;
035    
036        private TimeBasedTriggeringPolicy(final int interval, final boolean modulate) {
037            this.interval = interval;
038            this.modulate = modulate;
039        }
040    
041        /**
042         * Initialize the policy.
043         * @param manager The RollingFileManager.
044         */
045        @Override
046        public void initialize(final RollingFileManager manager) {
047            this.manager = manager;
048            nextRollover = manager.getProcessor().getNextTime(manager.getFileTime(), interval, modulate);
049        }
050    
051        /**
052         * Determine whether a rollover should occur.
053         * @param event   A reference to the currently event.
054         * @return true if a rollover should occur.
055         */
056        @Override
057        public boolean isTriggeringEvent(final LogEvent event) {
058            if (manager.getFileSize() == 0) {
059                return false;
060            }
061            final long now = System.currentTimeMillis();
062            if (now > nextRollover) {
063                nextRollover = manager.getProcessor().getNextTime(now, interval, modulate);
064                return true;
065            }
066            return false;
067        }
068    
069        @Override
070        public String toString() {
071            return "TimeBasedTriggeringPolicy";
072        }
073    
074        /**
075         * Create a TimeBasedTriggeringPolicy.
076         * @param interval The interval between rollovers.
077         * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
078         * @return a TimeBasedTriggeringPolicy.
079         */
080        @PluginFactory
081        public static TimeBasedTriggeringPolicy createPolicy(@PluginAttr("interval") final String interval,
082                                                             @PluginAttr("modulate") final String modulate) {
083            final int increment = interval == null ? 1 : Integer.parseInt(interval);
084            final boolean mod = modulate == null ? false : Boolean.parseBoolean(modulate);
085            return new TimeBasedTriggeringPolicy(increment, mod);
086        }
087    }