View Javadoc

1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.core.rolling;
12  
13  import java.io.File;
14  
15  import ch.qos.logback.core.util.FileSize;
16  
17  /**
18   * SizeBasedTriggeringPolicy looks at size of the file being
19   * currently written to. If it grows bigger than the specified size, 
20   * the FileAppender using the SizeBasedTriggeringPolicy rolls the file
21   * and creates a new one.
22   * 
23   * For more information about this policy, please refer to the online manual at
24   * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
25   * 
26   * @author Ceki Gülcü
27   *
28   */
29  public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
30    
31    public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
32    /**
33     * The default maximum file size.
34     */
35    public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
36    
37    String maxFileSizeAsString = Long.toString(DEFAULT_MAX_FILE_SIZE); 
38    FileSize maxFileSize;
39  
40    public SizeBasedTriggeringPolicy() {
41    }
42  
43    public SizeBasedTriggeringPolicy(final String maxFileSize) {
44        setMaxFileSize(maxFileSize);
45    }
46  
47    public boolean isTriggeringEvent(final File activeFile, final E event) {
48      //System.out.println("Size"+file.length());
49      return (activeFile.length() >= maxFileSize.getSize());
50    }
51  
52    public String getMaxFileSize() {
53      return maxFileSizeAsString;
54    }
55  
56    public void setMaxFileSize(String maxFileSize) {
57      this.maxFileSizeAsString = maxFileSize;
58      this.maxFileSize = FileSize.valueOf(maxFileSize);
59    }
60    
61    long toFileSize(String value) {
62      if(value == null)
63        return DEFAULT_MAX_FILE_SIZE;
64  
65      String s = value.trim().toUpperCase();
66      long multiplier = 1;
67      int index;
68  
69      if((index = s.indexOf("KB")) != -1) {
70        multiplier = 1024;
71        s = s.substring(0, index);
72      }
73      else if((index = s.indexOf("MB")) != -1) {
74        multiplier = 1024*1024;
75        s = s.substring(0, index);
76      }
77      else if((index = s.indexOf("GB")) != -1) {
78        multiplier = 1024*1024*1024;
79        s = s.substring(0, index);
80      }
81      if(s != null) {
82        try {
83          return Long.valueOf(s).longValue() * multiplier;
84        }
85        catch (NumberFormatException e) {
86          addError("[" + s + "] is not in proper int format. Please refer to "+SEE_SIZE_FORMAT);
87          addError("[" + value + "] not in expected format.", e);
88        }
89      }
90      return DEFAULT_MAX_FILE_SIZE;
91    } 
92  }