/
TimeBasedFileNamingAndTriggeringPolicyBase.java
154 lines (125 loc) · 4.86 KB
/
TimeBasedFileNamingAndTriggeringPolicyBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.core.rolling;
import static ch.qos.logback.core.CoreConstants.CODES_URL;
import java.io.File;
import java.time.Instant;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicLong;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.rolling.helper.ArchiveRemover;
import ch.qos.logback.core.rolling.helper.DateTokenConverter;
import ch.qos.logback.core.rolling.helper.RollingCalendar;
import ch.qos.logback.core.spi.ContextAwareBase;
/**
* Base implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}.
*
* <p>See also derived classes {@link DefaultTimeBasedFileNamingAndTriggeringPolicy} and
* {@link SizeAndTimeBasedFNATP}.
*
* </p>
*
* @param <E>
*/
abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends ContextAwareBase
implements TimeBasedFileNamingAndTriggeringPolicy<E> {
static private String COLLIDING_DATE_FORMAT_URL = CODES_URL + "#rfa_collision_in_dateFormat";
protected TimeBasedRollingPolicy<E> tbrp;
protected ArchiveRemover archiveRemover = null;
protected String elapsedPeriodsFileName;
protected RollingCalendar rc;
protected long artificialCurrentTime = -1;
protected AtomicLong atomicNextCheck = new AtomicLong(0);
protected Instant dateInCurrentPeriod = null;
protected boolean started = false;
protected boolean errorFree = true;
public boolean isStarted() {
return started;
}
public void start() {
DateTokenConverter<Object> dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter();
if (dtc == null) {
throw new IllegalStateException(
"FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken");
}
if (dtc.getZoneId() != null) {
TimeZone tz = TimeZone.getTimeZone(dtc.getZoneId());
rc = new RollingCalendar(dtc.getDatePattern(), tz, Locale.getDefault());
} else {
rc = new RollingCalendar(dtc.getDatePattern());
}
addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '"
+ tbrp.fileNamePattern.getPattern() + "'.");
rc.printPeriodicity(this);
if (!rc.isCollisionFree()) {
addError(
"The date format in FileNamePattern will result in collisions in the names of archived log files.");
addError(CoreConstants.MORE_INFO_PREFIX + COLLIDING_DATE_FORMAT_URL);
withErrors();
return;
}
long timestamp = getCurrentTime();
setDateInCurrentPeriod(timestamp);
if (tbrp.getParentsRawFileProperty() != null) {
File currentFile = new File(tbrp.getParentsRawFileProperty());
if (currentFile.exists() && currentFile.canRead()) {
timestamp = currentFile.lastModified();
setDateInCurrentPeriod(timestamp);
}
}
addInfo("Setting initial period to " + dateInCurrentPeriod);
long nextCheck = computeNextCheck(timestamp);
atomicNextCheck.set(nextCheck);
}
public void stop() {
started = false;
}
protected long computeNextCheck(long timestamp) {
return rc.getNextTriggeringDate(Instant.ofEpochMilli(timestamp)).toEpochMilli();
}
public String getElapsedPeriodsFileName() {
return elapsedPeriodsFileName;
}
public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
return tbrp.fileNamePatternWithoutCompSuffix.convert(dateInCurrentPeriod);
}
protected void setDateInCurrentPeriod(long timestamp) {
dateInCurrentPeriod = Instant.ofEpochMilli(timestamp);
}
public void setCurrentTime(long timeInMillis) {
artificialCurrentTime = timeInMillis;
}
public long getCurrentTime() {
// if time is forced return the time set by user
if (artificialCurrentTime >= 0) {
return artificialCurrentTime;
} else {
return System.currentTimeMillis();
}
}
public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
this.tbrp = _tbrp;
}
public ArchiveRemover getArchiveRemover() {
return archiveRemover;
}
protected void withErrors() {
errorFree = false;
}
protected boolean isErrorFree() {
return errorFree;
}
}