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.PluginAttr;
22  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23  
24  /**
25   * Triggering Policy that causes a rollover based on time.
26   */
27  @Plugin(name = "TimeBasedTriggeringPolicy", type = "Core", printObject = true)
28  public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
29  
30      private long nextRollover;
31      private final int interval;
32      private final boolean modulate;
33  
34      private RollingFileManager manager;
35  
36      private TimeBasedTriggeringPolicy(int interval, boolean modulate) {
37          this.interval = interval;
38          this.modulate = modulate;
39      }
40  
41      /**
42       * Initialize the policy.
43       * @param manager The RollingFileManager.
44       */
45      public void initialize(RollingFileManager manager) {
46          this.manager = manager;
47          nextRollover = manager.getProcessor().getNextTime(manager.getFileTime(), interval, modulate);
48      }
49  
50      /**
51       * Determine whether a rollover should occur.
52       * @param event   A reference to the currently event.
53       * @return true if a rollover should occur.
54       */
55      public boolean isTriggeringEvent(LogEvent event) {
56          if (manager.getFileSize() == 0) {
57              return false;
58          }
59          long now = System.currentTimeMillis();
60          if (now > nextRollover) {
61              nextRollover = manager.getProcessor().getNextTime(now, interval, modulate);
62              return true;
63          }
64          return false;
65      }
66  
67      @Override
68      public String toString() {
69          return "TimeBasedTriggeringPolicy";
70      }
71  
72      /**
73       * Create a TimeBasedTriggeringPolicy.
74       * @return a TimeBasedTriggeringPolicy.
75       */
76      @PluginFactory
77      public static TimeBasedTriggeringPolicy createPolicy(@PluginAttr("interval") String interval,
78                                                           @PluginAttr("modulate") String modulate) {
79          int increment = interval == null ? 1 : Integer.parseInt(interval);
80          boolean mod = modulate == null ? false : Boolean.parseBoolean(modulate);
81          return new TimeBasedTriggeringPolicy(increment, mod);
82      }
83  }