1
2
3
4
5
6
7
8
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
19
20
21
22
23
24
25
26
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
34
35 public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;
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
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 }