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