View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling;
15  
16  import java.io.File;
17  
18  import ch.qos.logback.core.util.Duration;
19  import ch.qos.logback.core.util.FileSize;
20  import ch.qos.logback.core.util.DefaultInvocationGate;
21  import ch.qos.logback.core.util.InvocationGate;
22  import ch.qos.logback.core.util.SimpleInvocationGate;
23  
24  /**
25   * SizeBasedTriggeringPolicy looks at size of the file being currently written
26   * to. If it grows bigger than the specified size, the FileAppender using the
27   * SizeBasedTriggeringPolicy rolls the file and creates a new one.
28   * 
29   * For more information about this policy, please refer to the online manual at
30   * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
31   * 
32   * @author Ceki Gülcü
33   * 
34   */
35  public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
36  
37      public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
38      /**
39       * The default maximum file size.
40       */
41      public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
42  
43      FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
44      InvocationGate invocationGate = new SimpleInvocationGate();
45      Duration checkIncrement = null;
46  
47      public SizeBasedTriggeringPolicy() {
48      }
49  
50      public void start() {
51          if(checkIncrement != null)
52              invocationGate = new SimpleInvocationGate(checkIncrement);
53          super.start();
54      }
55  
56  
57      public boolean isTriggeringEvent(final File activeFile, final E event) {
58          long now = System.currentTimeMillis();
59          if (invocationGate.isTooSoon(now))
60              return false;
61  
62          return (activeFile.length() >= maxFileSize.getSize());
63      }
64  
65      public FileSize getMaxFileSize() {
66          return this.maxFileSize;
67      }
68  
69      public void setMaxFileSize(FileSize aMaxFileSize) {
70          this.maxFileSize = aMaxFileSize;
71      }
72  
73      public Duration getCheckIncrement() {
74          return checkIncrement;
75      }
76  
77      public void setCheckIncrement(Duration checkIncrement) {
78          this.checkIncrement = checkIncrement;
79      }
80  }