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", type = "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(int interval, boolean modulate) {
037            this.interval = interval;
038            this.modulate = modulate;
039        }
040    
041        /**
042         * Initialize the policy.
043         * @param manager The RollingFileManager.
044         */
045        public void initialize(RollingFileManager manager) {
046            this.manager = manager;
047            nextRollover = manager.getProcessor().getNextTime(manager.getFileTime(), interval, modulate);
048        }
049    
050        /**
051         * Determine whether a rollover should occur.
052         * @param event   A reference to the currently event.
053         * @return true if a rollover should occur.
054         */
055        public boolean isTriggeringEvent(LogEvent event) {
056            if (manager.getFileSize() == 0) {
057                return false;
058            }
059            long now = System.currentTimeMillis();
060            if (now > nextRollover) {
061                nextRollover = manager.getProcessor().getNextTime(now, interval, modulate);
062                return true;
063            }
064            return false;
065        }
066    
067        @Override
068        public String toString() {
069            return "TimeBasedTriggeringPolicy";
070        }
071    
072        /**
073         * Create a TimeBasedTriggeringPolicy.
074         * @return a TimeBasedTriggeringPolicy.
075         */
076        @PluginFactory
077        public static TimeBasedTriggeringPolicy createPolicy(@PluginAttr("interval") String interval,
078                                                             @PluginAttr("modulate") String modulate) {
079            int increment = interval == null ? 1 : Integer.parseInt(interval);
080            boolean mod = modulate == null ? false : Boolean.parseBoolean(modulate);
081            return new TimeBasedTriggeringPolicy(increment, mod);
082        }
083    }