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