View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling;
18  
19  import org.apache.logging.log4j.core.LogEvent;
20  import org.apache.logging.log4j.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
22  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23  import org.apache.logging.log4j.core.util.Integers;
24  
25  /**
26   * Rolls a file over based on time.
27   */
28  @Plugin(name = "TimeBasedTriggeringPolicy", category = "Core", printObject = true)
29  public final class TimeBasedTriggeringPolicy extends AbstractTriggeringPolicy {
30  
31      private long nextRolloverMillis;
32      private final int interval;
33      private final boolean modulate;
34  
35      private RollingFileManager manager;
36  
37      private TimeBasedTriggeringPolicy(final int interval, final boolean modulate) {
38          this.interval = interval;
39          this.modulate = modulate;
40      }
41  
42      public int getInterval() {
43          return interval;
44      }
45  
46      public long getNextRolloverMillis() {
47          return nextRolloverMillis;
48      }
49  
50      /**
51       * Initializes the policy.
52       * @param aManager The RollingFileManager.
53       */
54      @Override
55      public void initialize(final RollingFileManager aManager) {
56          this.manager = aManager;
57          
58          // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
59          aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), interval, modulate);
60          
61          nextRolloverMillis = aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), interval, modulate);
62      }
63  
64      /**
65       * Determines whether a rollover should occur.
66       * @param event   A reference to the currently event.
67       * @return true if a rollover should occur.
68       */
69      @Override
70      public boolean isTriggeringEvent(final LogEvent event) {
71          if (manager.getFileSize() == 0) {
72              return false;
73          }
74          final long nowMillis = event.getTimeMillis();
75          if (nowMillis >= nextRolloverMillis) {
76              nextRolloverMillis = manager.getPatternProcessor().getNextTime(nowMillis, interval, modulate);
77              return true;
78          }
79          return false;
80      }
81  
82      /**
83       * Creates a TimeBasedTriggeringPolicy.
84       * @param interval The interval between rollovers.
85       * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
86       * @return a TimeBasedTriggeringPolicy.
87       */
88      @PluginFactory
89      public static TimeBasedTriggeringPolicy createPolicy(
90              @PluginAttribute("interval") final String interval,
91              @PluginAttribute("modulate") final String modulate) {
92          final int increment = Integers.parseInt(interval, 1);
93          final boolean mod = Boolean.parseBoolean(modulate);
94          return new TimeBasedTriggeringPolicy(increment, mod);
95      }
96  
97      @Override
98      public String toString() {
99          return "TimeBasedTriggeringPolicy(nextRolloverMillis=" + nextRolloverMillis + ", interval=" + interval
100                 + ", modulate=" + modulate + ")";
101     }
102 
103 }