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.Logger;
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.status.StatusLogger;
25  
26  /**
27   *
28   */
29  @Plugin(name = "SizeBasedTriggeringPolicy", category = "Core", printObject = true)
30  public class SizeBasedTriggeringPolicy implements TriggeringPolicy {
31      /**
32       * Allow subclasses access to the status logger without creating another instance.
33       */
34      protected static final Logger LOGGER = StatusLogger.getLogger();
35  
36      /**
37       * Rollover threshold size in bytes.
38       */
39      private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // let 10 MB the default max size
40  
41      private final long maxFileSize;
42  
43      private RollingFileManager manager;
44  
45      /**
46       * Constructs a new instance.
47       */
48      protected SizeBasedTriggeringPolicy() {
49          this.maxFileSize = MAX_FILE_SIZE;
50      }
51  
52      /**
53       * Constructs a new instance.
54       *
55       * @param maxFileSize rollover threshold size in bytes.
56       */
57      protected SizeBasedTriggeringPolicy(final long maxFileSize) {
58          this.maxFileSize = maxFileSize;
59      }
60  
61      public long getMaxFileSize() {
62          return maxFileSize;
63      }
64  
65      /**
66       * Initialize the TriggeringPolicy.
67       * @param aManager The RollingFileManager.
68       */
69      @Override
70      public void initialize(final RollingFileManager aManager) {
71          this.manager = aManager;
72      }
73  
74  
75      /**
76       * Returns true if a rollover should occur.
77       * @param event   A reference to the currently event.
78       * @return true if a rollover should take place, false otherwise.
79       */
80      @Override
81      public boolean isTriggeringEvent(final LogEvent event) {
82          final boolean triggered = manager.getFileSize() > maxFileSize;
83          if (triggered) {
84              manager.getPatternProcessor().updateTime();
85          }
86          return triggered;
87      }
88  
89      @Override
90      public String toString() {
91          return "SizeBasedTriggeringPolicy(size=" + maxFileSize + ')';
92      }
93  
94      /**
95       * Create a SizeBasedTriggeringPolicy.
96       * @param size The size of the file before rollover is required.
97       * @return A SizeBasedTriggeringPolicy.
98       */
99      @PluginFactory
100     public static SizeBasedTriggeringPolicy createPolicy(@PluginAttribute("size") final String size) {
101 
102         final long maxSize = size == null ? MAX_FILE_SIZE : FileSize.parse(size, MAX_FILE_SIZE);
103         return new SizeBasedTriggeringPolicy(maxSize);
104     }
105 
106 }