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.CODES_URL;
17
18 import java.io.File;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.util.Locale;
22 import java.util.TimeZone;
23 import java.util.concurrent.atomic.AtomicLong;
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.DateTokenConverter;
28 import ch.qos.logback.core.rolling.helper.RollingCalendar;
29 import ch.qos.logback.core.spi.ContextAwareBase;
30
31
32
33
34
35
36
37
38
39
40
41 abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends ContextAwareBase
42 implements TimeBasedFileNamingAndTriggeringPolicy<E> {
43
44 static private String COLLIDING_DATE_FORMAT_URL = CODES_URL + "#rfa_collision_in_dateFormat";
45
46 protected TimeBasedRollingPolicy<E> enclosingTBRP;
47
48 protected ArchiveRemover archiveRemover = null;
49 protected String elapsedPeriodsFileName;
50 protected RollingCalendar rc;
51
52 protected long artificialCurrentTime = -1;
53
54 protected AtomicLong atomicNextCheck = new AtomicLong(0);
55 protected Instant dateInCurrentPeriod = null;
56
57 protected boolean started = false;
58 protected boolean errorFree = true;
59
60 protected ZoneId zoneId = ZoneId.systemDefault();
61
62 public boolean isStarted() {
63 return started;
64 }
65
66 public void start() {
67 DateTokenConverter<Object> dtc = enclosingTBRP.fileNamePattern.getPrimaryDateTokenConverter();
68 if (dtc == null) {
69 throw new IllegalStateException(
70 "FileNamePattern [" + enclosingTBRP.fileNamePattern.getPattern() + "] does not contain a valid DateToken");
71 }
72
73 if (dtc.getZoneId() != null) {
74 this.zoneId = dtc.getZoneId();
75 TimeZone tz = TimeZone.getTimeZone(zoneId);
76 rc = new RollingCalendar(dtc.getDatePattern(), tz, Locale.getDefault());
77 } else {
78 rc = new RollingCalendar(dtc.getDatePattern());
79 }
80 addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '"
81 + enclosingTBRP.fileNamePattern.getPattern() + "'.");
82 rc.printPeriodicity(this);
83
84 if (!rc.isCollisionFree()) {
85 addError(
86 "The date format in FileNamePattern will result in collisions in the names of archived log files.");
87 addError(CoreConstants.MORE_INFO_PREFIX + COLLIDING_DATE_FORMAT_URL);
88 withErrors();
89 return;
90 }
91
92 long timestamp = getCurrentTime();
93 setDateInCurrentPeriod(timestamp);
94
95 if (enclosingTBRP.getParentsRawFileProperty() != null) {
96 File currentFile = new File(enclosingTBRP.getParentsRawFileProperty());
97 if (currentFile.canRead()) {
98 long lastModified = currentFile.lastModified();
99 if(lastModified < timestamp) {
100 timestamp = lastModified;
101 } else {
102 addWarn("Current time is earlier than last modified of file [" + currentFile.getAbsolutePath() + "]. Ignoring last modified and using current time.");
103 }
104 setDateInCurrentPeriod(timestamp);
105 }
106 }
107 addInfo("Setting initial period to " + dateInCurrentPeriod);
108 long nextCheck = computeNextCheck(timestamp);
109 atomicNextCheck.set(nextCheck);
110 }
111
112 public void stop() {
113 started = false;
114 }
115
116 protected long computeNextCheck(long timestamp) {
117 return rc.getNextTriggeringDate(Instant.ofEpochMilli(timestamp)).toEpochMilli();
118 }
119
120 public String getElapsedPeriodsFileName() {
121 return elapsedPeriodsFileName;
122 }
123
124 public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
125 return enclosingTBRP.fileNamePatternWithoutCompSuffix.convert(dateInCurrentPeriod);
126 }
127
128 protected void setDateInCurrentPeriod(long timestamp) {
129 dateInCurrentPeriod = Instant.ofEpochMilli(timestamp);
130 }
131
132 public void setCurrentTime(long timeInMillis) {
133 artificialCurrentTime = timeInMillis;
134 }
135
136 public long getCurrentTime() {
137
138 if (artificialCurrentTime >= 0) {
139 return artificialCurrentTime;
140 } else {
141 return System.currentTimeMillis();
142 }
143 }
144
145 public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
146 this.enclosingTBRP = _tbrp;
147
148 }
149
150 public ArchiveRemover getArchiveRemover() {
151 return archiveRemover;
152 }
153
154 protected void withErrors() {
155 errorFree = false;
156 }
157
158 protected boolean isErrorFree() {
159 return errorFree;
160 }
161
162 }