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", category = "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(final int interval, final boolean modulate) {
37          this.interval = interval;
38          this.modulate = modulate;
39      }
40  
41      /**
42       * Initialize the policy.
43       * @param manager The RollingFileManager.
44       */
45      @Override
46      public void initialize(final RollingFileManager manager) {
47          this.manager = manager;
48          nextRollover = manager.getProcessor().getNextTime(manager.getFileTime(), interval, modulate);
49      }
50  
51      /**
52       * Determine whether a rollover should occur.
53       * @param event   A reference to the currently event.
54       * @return true if a rollover should occur.
55       */
56      @Override
57      public boolean isTriggeringEvent(final LogEvent event) {
58          if (manager.getFileSize() == 0) {
59              return false;
60          }
61          final long now = System.currentTimeMillis();
62          if (now > nextRollover) {
63              nextRollover = manager.getProcessor().getNextTime(now, interval, modulate);
64              return true;
65          }
66          return false;
67      }
68  
69      @Override
70      public String toString() {
71          return "TimeBasedTriggeringPolicy";
72      }
73  
74      /**
75       * Create a TimeBasedTriggeringPolicy.
76       * @param interval The interval between rollovers.
77       * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
78       * @return a TimeBasedTriggeringPolicy.
79       */
80      @PluginFactory
81      public static TimeBasedTriggeringPolicy createPolicy(@PluginAttr("interval") final String interval,
82                                                           @PluginAttr("modulate") final String modulate) {
83          final int increment = interval == null ? 1 : Integer.parseInt(interval);
84          final boolean mod = modulate == null ? false : Boolean.parseBoolean(modulate);
85          return new TimeBasedTriggeringPolicy(increment, mod);
86      }
87  }