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   * Triggering Policy that causes a rollover based on time.
27   */
28  @Plugin(name = "TimeBasedTriggeringPolicy", category = "Core", printObject = true)
29  public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
30  
31      private long nextRollover;
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      /**
43       * Initialize the policy.
44       * @param manager The RollingFileManager.
45       */
46      @Override
47      public void initialize(final RollingFileManager manager) {
48          this.manager = manager;
49          
50          // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
51          manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
52          
53          nextRollover = manager.getPatternProcessor().getNextTime(manager.getFileTime(), interval, modulate);
54      }
55  
56      /**
57       * Determine whether a rollover should occur.
58       * @param event   A reference to the currently event.
59       * @return true if a rollover should occur.
60       */
61      @Override
62      public boolean isTriggeringEvent(final LogEvent event) {
63          if (manager.getFileSize() == 0) {
64              return false;
65          }
66          final long now = event.getTimeMillis();
67          if (now > nextRollover) {
68              nextRollover = manager.getPatternProcessor().getNextTime(now, interval, modulate);
69              return true;
70          }
71          return false;
72      }
73  
74      @Override
75      public String toString() {
76          return "TimeBasedTriggeringPolicy";
77      }
78  
79      /**
80       * Create a TimeBasedTriggeringPolicy.
81       * @param interval The interval between rollovers.
82       * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
83       * @return a TimeBasedTriggeringPolicy.
84       */
85      @PluginFactory
86      public static TimeBasedTriggeringPolicy createPolicy(
87              @PluginAttribute("interval") final String interval,
88              @PluginAttribute("modulate") final String modulate) {
89          final int increment = Integers.parseInt(interval, 1);
90          final boolean mod = Boolean.parseBoolean(modulate);
91          return new TimeBasedTriggeringPolicy(increment, mod);
92      }
93  }