1
2
3
4
5
6
7
8
9
10
11
12 package ch.qos.logback.core.rolling;
13
14 import static ch.qos.logback.core.CoreConstants.CODES_URL;
15 import static ch.qos.logback.core.CoreConstants.MORE_INFO_PREFIX;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.concurrent.locks.Lock;
22 import java.util.concurrent.locks.ReentrantLock;
23 import java.util.regex.PatternSyntaxException;
24
25 import ch.qos.logback.core.CoreConstants;
26 import ch.qos.logback.core.FileAppender;
27 import ch.qos.logback.core.rolling.helper.CompressionMode;
28 import ch.qos.logback.core.rolling.helper.FileNamePattern;
29 import ch.qos.logback.core.util.ContextUtil;
30
31
32
33
34
35
36
37
38
39
40
41
42 public class RollingFileAppender<E> extends FileAppender<E> {
43 File currentlyActiveFile;
44 TriggeringPolicy<E> triggeringPolicy;
45 RollingPolicy rollingPolicy;
46
47 Lock triggeringPolicyLock = new ReentrantLock();
48
49 static private String RFA_NO_TP_URL = CODES_URL + "#rfa_no_tp";
50 static private String RFA_NO_RP_URL = CODES_URL + "#rfa_no_rp";
51 static private String COLLISION_URL = CODES_URL + "#rfa_collision";
52 static private String RFA_LATE_FILE_URL = CODES_URL + "#rfa_file_after";
53 static private String RFA_RESET_RP_OR_TP = CODES_URL + "#rfa_reset_rp_or_tp";
54
55 public void start() {
56 if (triggeringPolicy == null) {
57 addWarn("No TriggeringPolicy was set for the RollingFileAppender named " + getName());
58 addWarn(MORE_INFO_PREFIX + RFA_NO_TP_URL);
59 return;
60 }
61 if (!triggeringPolicy.isStarted()) {
62 addWarn("TriggeringPolicy has not started. RollingFileAppender will not start");
63 return;
64 }
65
66
67
68
69
70
71
72
73 if (!append) {
74 addWarn("Append mode is mandatory for RollingFileAppender. Defaulting to append=true.");
75 append = true;
76 }
77
78 if (rollingPolicy == null) {
79 addError("No RollingPolicy was set for the RollingFileAppender named " + getName());
80 addError(MORE_INFO_PREFIX + RFA_NO_RP_URL);
81 return;
82 }
83
84
85 if (checkForFileAndPatternCollisions()) {
86 addError("File property collides with fileNamePattern. Aborting.");
87 addError(MORE_INFO_PREFIX + COLLISION_URL);
88 return;
89 }
90
91 if (isPrudent()) {
92 if (rawFileProperty() != null) {
93 addWarn("Setting \"File\" property to null on account of prudent mode");
94 setFile(null);
95 }
96 if (rollingPolicy.getCompressionMode() != CompressionMode.NONE) {
97 addError("Compression is not supported in prudent mode. Aborting");
98 return;
99 }
100 }
101
102 addInfo("Active log file name: " + getFile());
103 currentlyActiveFile = new File(getFile());
104 initializeLengthCounter();
105 super.start();
106 }
107
108
109
110 private boolean checkForFileAndPatternCollisions() {
111 if (triggeringPolicy instanceof RollingPolicyBase) {
112 final RollingPolicyBase base = (RollingPolicyBase) triggeringPolicy;
113 final FileNamePattern fileNamePattern = base.fileNamePattern;
114
115 if (fileNamePattern != null && fileName != null) {
116 String regex = fileNamePattern.toRegex();
117 try {
118 return fileName.matches(regex);
119 } catch (PatternSyntaxException e) {
120
121 addWarn("Could not check for collisions between file property [" + fileName
122 + "] and fileNamePattern [" + fileNamePattern + "] using regex [" + regex + "]", e);
123 return false;
124 }
125 }
126 }
127 return false;
128 }
129
130 private void initializeLengthCounter() {
131 if(getLengthCounter() != null && currentlyActiveFile.exists()) {
132 long currentFileLength = currentlyActiveFile.length();
133 addInfo("Setting currentFileLength to "+currentFileLength+ " for "+currentlyActiveFile);
134 incrementByteCount(currentFileLength);
135 }
136 }
137
138 @Override
139 public void stop() {
140 if (!isStarted()) {
141 return;
142 }
143 super.stop();
144
145 if (rollingPolicy != null)
146 rollingPolicy.stop();
147 if (triggeringPolicy != null)
148 triggeringPolicy.stop();
149 }
150
151 @Override
152 public void setFile(String file) {
153
154
155 if (file != null && ((triggeringPolicy != null) || (rollingPolicy != null))) {
156 addError("File property must be set before any triggeringPolicy or rollingPolicy properties");
157 addError(MORE_INFO_PREFIX + RFA_LATE_FILE_URL);
158 }
159 super.setFile(file);
160 }
161
162 @Override
163 public String getFile() {
164 return rollingPolicy.getActiveFileName();
165 }
166
167
168
169
170 public void rollover() {
171 streamWriteLock.lock();
172 try {
173
174
175
176
177
178 this.closeOutputStream();
179 attemptRollover();
180 attemptOpenFile();
181 } finally {
182 streamWriteLock.unlock();
183 }
184 }
185
186 private void attemptOpenFile() {
187 try {
188
189 currentlyActiveFile = new File(rollingPolicy.getActiveFileName());
190
191
192
193 this.openFile(rollingPolicy.getActiveFileName());
194 } catch (IOException e) {
195 addError("setFile(" + fileName + ", false) call failed.", e);
196 }
197 }
198
199 private void attemptRollover() {
200 try {
201 rollingPolicy.rollover();
202 } catch (RolloverFailure rf) {
203 addWarn("RolloverFailure occurred. Deferring roll-over.");
204
205 this.append = true;
206 }
207 }
208
209
210
211
212 @Override
213 protected void subAppend(E event) {
214
215
216
217
218
219
220
221
222 triggeringPolicyLock.lock();
223 try {
224 if (triggeringPolicy.isTriggeringEvent(currentlyActiveFile, event)) {
225 rollover();
226 }
227 } finally {
228 triggeringPolicyLock.unlock();
229 }
230
231 super.subAppend(event);
232 }
233
234 public RollingPolicy getRollingPolicy() {
235 return rollingPolicy;
236 }
237
238 public TriggeringPolicy<E> getTriggeringPolicy() {
239 return triggeringPolicy;
240 }
241
242
243
244
245
246
247
248
249 @SuppressWarnings("unchecked")
250 public void setRollingPolicy(RollingPolicy policy) {
251 if (this.rollingPolicy instanceof TriggeringPolicy) {
252 String className = rollingPolicy.getClass().getSimpleName();
253 addWarn("A rolling policy of type " + className + " was already set.");
254 addWarn("Note that " + className + " doubles as a TriggeringPolicy");
255 addWarn("See also " + RFA_RESET_RP_OR_TP);
256 }
257 this.rollingPolicy = policy;
258 if (this.rollingPolicy instanceof TriggeringPolicy) {
259 this.triggeringPolicy = (TriggeringPolicy<E>) policy;
260 }
261
262 }
263
264 public void setTriggeringPolicy(TriggeringPolicy<E> policy) {
265 if (triggeringPolicy instanceof RollingPolicy) {
266 String className = triggeringPolicy.getClass().getSimpleName();
267 addWarn("A triggering policy of type " + className + " was already set.");
268 addWarn("Note that " + className + " doubles as a RollingPolicy");
269 addWarn("See also " + RFA_RESET_RP_OR_TP);
270 }
271 triggeringPolicy = policy;
272 if (policy instanceof RollingPolicy) {
273 rollingPolicy = (RollingPolicy) policy;
274 }
275 }
276
277 @Override
278 protected void updateByteCount(byte[] byteArray) {
279 if(byteArray == null)
280 return;
281 incrementByteCount(byteArray.length);
282 }
283
284 void incrementByteCount(long increment) {
285 LengthCounter lengthCounter = getLengthCounter();
286 if (lengthCounter == null)
287 return;
288
289 if (increment > 0) {
290 lengthCounter.add(increment);
291 }
292 }
293
294 private LengthCounter getLengthCounter() {
295 return triggeringPolicy.getLengthCounter();
296 }
297
298 }