1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.rolling;
15
16 import static ch.qos.logback.core.CoreConstants.UNBOUNDED_HISTORY;
17 import static ch.qos.logback.core.CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP;
18
19 import java.io.File;
20 import java.time.Instant;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24
25 import ch.qos.logback.core.CoreConstants;
26 import ch.qos.logback.core.rolling.helper.ArchiveRemover;
27 import ch.qos.logback.core.rolling.helper.CompressionMode;
28 import ch.qos.logback.core.rolling.helper.Compressor;
29 import ch.qos.logback.core.rolling.helper.FileFilterUtil;
30 import ch.qos.logback.core.rolling.helper.FileNamePattern;
31 import ch.qos.logback.core.rolling.helper.RenameUtil;
32 import ch.qos.logback.core.util.FileSize;
33
34
35
36
37
38
39
40
41
42
43
44
45 public class TimeBasedRollingPolicy<E> extends RollingPolicyBase implements TriggeringPolicy<E> {
46 static final String FNP_NOT_SET = "The FileNamePattern option must be set before using TimeBasedRollingPolicy. ";
47
48 FileNamePattern fileNamePatternWithoutCompSuffix;
49
50 private Compressor compressor;
51 private final RenameUtil renameUtil = new RenameUtil();
52 Future<?> compressionFuture;
53 Future<?> cleanUpFuture;
54
55 private int maxHistory = UNBOUNDED_HISTORY;
56 protected FileSize totalSizeCap = new FileSize(UNBOUNDED_TOTAL_SIZE_CAP);
57
58 private ArchiveRemover archiveRemover;
59
60 TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedFileNamingAndTriggeringPolicy;
61
62 boolean cleanHistoryOnStart = false;
63
64 public void start() {
65
66 renameUtil.setContext(this.context);
67
68
69 if (fileNamePatternStr != null) {
70 compressionMode = fileNamePatternStrToCompressionMode(fileNamePatternStr);
71 outputCompressionModeMessage(compressionMode);
72 adjustCompressionModeAndFileNamePatternStrIfNecessary();
73 fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
74 } else {
75 addWarn(FNP_NOT_SET);
76 addWarn(CoreConstants.SEE_FNP_NOT_SET);
77 throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET);
78 }
79
80 compressor = new Compressor(compressionMode);
81 compressor.setContext(context);
82
83
84 fileNamePatternWithoutCompSuffix = new FileNamePattern(
85 Compressor.computeFileNameStrWithoutCompSuffix(fileNamePatternStr, compressionMode), this.context);
86
87 addInfo("Will use the FileNamePattern [" + fileNamePatternWithoutCompSuffix + "]");
88
89 if (compressionMode == CompressionMode.ZIP) {
90 String zipEntryFileNamePatternStr = transformFileNamePattern2ZipEntry(fileNamePatternStr);
91 zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context);
92 }
93
94 if (timeBasedFileNamingAndTriggeringPolicy == null) {
95 timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
96 }
97 timeBasedFileNamingAndTriggeringPolicy.setContext(context);
98 timeBasedFileNamingAndTriggeringPolicy.setTimeBasedRollingPolicy(this);
99 timeBasedFileNamingAndTriggeringPolicy.start();
100
101 if (!timeBasedFileNamingAndTriggeringPolicy.isStarted()) {
102 addWarn("Subcomponent did not start. TimeBasedRollingPolicy will not start.");
103 return;
104 }
105
106
107
108
109 if (maxHistory != UNBOUNDED_HISTORY) {
110 archiveRemover = timeBasedFileNamingAndTriggeringPolicy.getArchiveRemover();
111 archiveRemover.setMaxHistory(maxHistory);
112 archiveRemover.setTotalSizeCap(totalSizeCap.getSize());
113 if (cleanHistoryOnStart) {
114 addInfo("Cleaning on start up");
115 Instant now = Instant.ofEpochMilli(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
116 cleanUpFuture = archiveRemover.cleanAsynchronously(now);
117 }
118 } else if (!isUnboundedTotalSizeCap()) {
119 addWarn("'maxHistory' is not set, ignoring 'totalSizeCap' option with value [" + totalSizeCap + "]");
120 }
121
122 super.start();
123 }
124
125 protected boolean isUnboundedTotalSizeCap() {
126 return totalSizeCap.getSize() == UNBOUNDED_TOTAL_SIZE_CAP;
127 }
128
129 @Override
130 public void stop() {
131 if (!isStarted())
132 return;
133 waitForAsynchronousJobToStop(compressionFuture, "compression");
134 waitForAsynchronousJobToStop(cleanUpFuture, "clean-up");
135 super.stop();
136 }
137
138 private void waitForAsynchronousJobToStop(Future<?> aFuture, String jobDescription) {
139 if (aFuture != null) {
140 try {
141 aFuture.get(CoreConstants.SECONDS_TO_WAIT_FOR_COMPRESSION_JOBS, TimeUnit.SECONDS);
142 } catch (TimeoutException e) {
143 addError("Timeout while waiting for " + jobDescription + " job to finish", e);
144 } catch (Exception e) {
145 addError("Unexpected exception while waiting for " + jobDescription + " job to finish", e);
146 }
147 }
148 }
149
150 private String transformFileNamePattern2ZipEntry(String fileNamePatternStr) {
151 String slashified = FileFilterUtil.slashify(fileNamePatternStr);
152 return FileFilterUtil.afterLastSlash(slashified);
153 }
154
155 public void setTimeBasedFileNamingAndTriggeringPolicy(
156 TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering) {
157 this.timeBasedFileNamingAndTriggeringPolicy = timeBasedTriggering;
158 }
159
160 public TimeBasedFileNamingAndTriggeringPolicy<E> getTimeBasedFileNamingAndTriggeringPolicy() {
161 return timeBasedFileNamingAndTriggeringPolicy;
162 }
163
164 public void rollover() throws RolloverFailure {
165
166
167
168
169 String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();
170
171 String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);
172
173 if (compressionMode == CompressionMode.NONE) {
174 if (getParentsRawFileProperty() != null) {
175 renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
176 }
177
178 } else {
179 if (getParentsRawFileProperty() == null) {
180 compressionFuture = compressor.asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName,
181 elapsedPeriodStem);
182 } else {
183 compressionFuture = renameRawAndAsyncCompress(elapsedPeriodsFileName, elapsedPeriodStem);
184 }
185 }
186
187 if (archiveRemover != null) {
188 Instant now = Instant.ofEpochMilli(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
189 this.cleanUpFuture = archiveRemover.cleanAsynchronously(now);
190 }
191 }
192
193 Future<?> renameRawAndAsyncCompress(String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
194 String parentsRawFile = getParentsRawFileProperty();
195
196 String tmpTarget = Compressor.computeFileNameStrWithoutCompSuffix(nameOfCompressedFile, compressionMode);
197 renameUtil.rename(parentsRawFile, tmpTarget);
198 return compressor.asyncCompress(tmpTarget, nameOfCompressedFile, innerEntryName);
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public String getActiveFileName() {
224 String parentsRawFileProperty = getParentsRawFileProperty();
225 if (parentsRawFileProperty != null) {
226 return parentsRawFileProperty;
227 } else {
228 return timeBasedFileNamingAndTriggeringPolicy.getCurrentPeriodsFileNameWithoutCompressionSuffix();
229 }
230 }
231
232
233
234
235
236
237
238
239 public boolean isTriggeringEvent(File activeFile, final E event) {
240 return timeBasedFileNamingAndTriggeringPolicy.isTriggeringEvent(activeFile, event);
241 }
242
243 @Override
244 public LengthCounter getLengthCounter() {
245 return timeBasedFileNamingAndTriggeringPolicy.getLengthCounter();
246 }
247
248
249
250
251
252
253 public int getMaxHistory() {
254 return maxHistory;
255 }
256
257
258
259
260
261
262 public void setMaxHistory(int maxHistory) {
263 this.maxHistory = maxHistory;
264 }
265
266 public boolean isCleanHistoryOnStart() {
267 return cleanHistoryOnStart;
268 }
269
270
271
272
273
274
275
276
277 public void setCleanHistoryOnStart(boolean cleanHistoryOnStart) {
278 this.cleanHistoryOnStart = cleanHistoryOnStart;
279 }
280
281 @Override
282 public String toString() {
283 return "c.q.l.core.rolling.TimeBasedRollingPolicy@" + this.hashCode();
284 }
285
286 public void setTotalSizeCap(FileSize totalSizeCap) {
287 addInfo("setting totalSizeCap to " + totalSizeCap.toString());
288 this.totalSizeCap = totalSizeCap;
289 }
290 }