1
2
3
4
5
6
7
8
9
10
11
12
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
22
23
24
25
26
27
28
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
36
37 public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;
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
50
51
52
53 private int invocationCounter;
54
55 public boolean isTriggeringEvent(final File activeFile, final E event) {
56
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 }