View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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.FileSize;
19  
20  /**
21   * SizeBasedTriggeringPolicy looks at size of the file being currently written
22   * to. If it grows bigger than the specified size, the FileAppender using the
23   * SizeBasedTriggeringPolicy rolls the file and creates a new one.
24   * 
25   * For more information about this policy, please refer to the online manual at
26   * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
27   * 
28   * @author Ceki Gülcü
29   * 
30   */
31  public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
32  
33    public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
34    /**
35     * The default maximum file size.
36     */
37    public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
38  
39    String maxFileSizeAsString = Long.toString(DEFAULT_MAX_FILE_SIZE);
40    FileSize maxFileSize;
41  
42    public SizeBasedTriggeringPolicy() {
43    }
44  
45    public SizeBasedTriggeringPolicy(final String maxFileSize) {
46      setMaxFileSize(maxFileSize);
47    }
48  
49    // IMPORTANT: This field can be updated by multiple threads. It follows that
50    // its values may *not* be incremented sequentially. However, we don't care
51    // about the actual value of the field except that from time to time the
52    // expression (invocationCounter++ & 0xF) == 0xF) should be true.
53    private int invocationCounter;
54  
55    public boolean isTriggeringEvent(final File activeFile, final E event) {
56      // for performance reasons, check for changes every 16 invocations
57      if (((invocationCounter++) & 0xF) != 0xF) {
58        return false;
59      }
60  
61      return (activeFile.length() >= maxFileSize.getSize());
62    }
63  
64    public String getMaxFileSize() {
65      return maxFileSizeAsString;
66    }
67  
68    public void setMaxFileSize(String maxFileSize) {
69      this.maxFileSizeAsString = maxFileSize;
70      this.maxFileSize = FileSize.valueOf(maxFileSize);
71    }
72  
73    long toFileSize(String value) {
74      if (value == null)
75        return DEFAULT_MAX_FILE_SIZE;
76  
77      String s = value.trim().toUpperCase();
78      long multiplier = 1;
79      int index;
80  
81      if ((index = s.indexOf("KB")) != -1) {
82        multiplier = 1024;
83        s = s.substring(0, index);
84      } else if ((index = s.indexOf("MB")) != -1) {
85        multiplier = 1024 * 1024;
86        s = s.substring(0, index);
87      } else if ((index = s.indexOf("GB")) != -1) {
88        multiplier = 1024 * 1024 * 1024;
89        s = s.substring(0, index);
90      }
91      if (s != null) {
92        try {
93          return Long.valueOf(s).longValue() * multiplier;
94        } catch (NumberFormatException e) {
95          addError("[" + s + "] is not in proper int format. Please refer to "
96              + SEE_SIZE_FORMAT);
97          addError("[" + value + "] not in expected format.", e);
98        }
99      }
100     return DEFAULT_MAX_FILE_SIZE;
101   }
102 }