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.Core;
20  import org.apache.logging.log4j.core.LogEvent;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
23  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
24  import org.apache.logging.log4j.core.util.Integers;
25  
26  /**
27   * Rolls a file over based on time.
28   */
29  @Plugin(name = "TimeBasedTriggeringPolicy", category = Core.CATEGORY_NAME, printObject = true)
30  public final class TimeBasedTriggeringPolicy extends AbstractTriggeringPolicy {
31  
32      private long nextRolloverMillis;
33      private final int interval;
34      private final boolean modulate;
35  
36      private RollingFileManager manager;
37  
38      private TimeBasedTriggeringPolicy(final int interval, final boolean modulate) {
39          this.interval = interval;
40          this.modulate = modulate;
41      }
42  
43      public int getInterval() {
44          return interval;
45      }
46  
47      public long getNextRolloverMillis() {
48          return nextRolloverMillis;
49      }
50  
51      /**
52       * Initializes the policy.
53       * @param aManager The RollingFileManager.
54       */
55      @Override
56      public void initialize(final RollingFileManager aManager) {
57          this.manager = aManager;
58          
59          // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
60          aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), interval, modulate);
61          
62          nextRolloverMillis = aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), interval, modulate);
63      }
64  
65      /**
66       * Determines whether a rollover should occur.
67       * @param event   A reference to the currently event.
68       * @return true if a rollover should occur.
69       */
70      @Override
71      public boolean isTriggeringEvent(final LogEvent event) {
72          if (manager.getFileSize() == 0) {
73              return false;
74          }
75          final long nowMillis = event.getTimeMillis();
76          if (nowMillis >= nextRolloverMillis) {
77              nextRolloverMillis = manager.getPatternProcessor().getNextTime(nowMillis, interval, modulate);
78              return true;
79          }
80          return false;
81      }
82  
83      /**
84       * Creates a TimeBasedTriggeringPolicy.
85       * @param interval The interval between rollovers.
86       * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
87       * @return a TimeBasedTriggeringPolicy.
88       */
89      @PluginFactory
90      public static TimeBasedTriggeringPolicy createPolicy(
91              @PluginAttribute("interval") final String interval,
92              @PluginAttribute("modulate") final String modulate) {
93          final int increment = Integers.parseInt(interval, 1);
94          final boolean mod = Boolean.parseBoolean(modulate);
95          return new TimeBasedTriggeringPolicy(increment, mod);
96      }
97  
98      @Override
99      public String toString() {
100         return "TimeBasedTriggeringPolicy(nextRolloverMillis=" + nextRolloverMillis + ", interval=" + interval
101                 + ", modulate=" + modulate + ")";
102     }
103 
104 }