1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.rolling;
18
19 import java.io.File;
20 import java.util.Date;
21
22 import org.apache.log4j.Appender;
23 import org.apache.log4j.pattern.PatternConverter;
24 import org.apache.log4j.rolling.helper.Action;
25 import org.apache.log4j.rolling.helper.FileRenameAction;
26 import org.apache.log4j.rolling.helper.GZCompressAction;
27 import org.apache.log4j.rolling.helper.ZipCompressAction;
28 import org.apache.log4j.spi.LoggingEvent;
29
30
31 /***
32 * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite
33 * powerful.
34 *
35 * <p>In order to use <code>TimeBasedRollingPolicy</code>, the
36 * <b>FileNamePattern</b> option must be set. It basically specifies the name of the
37 * rolled log files. The value <code>FileNamePattern</code> should consist of
38 * the name of the file, plus a suitably placed <code>%d</code> conversion
39 * specifier. The <code>%d</code> conversion specifier may contain a date and
40 * time pattern as specified by the {@link java.text.SimpleDateFormat} class. If
41 * the date and time pattern is ommitted, then the default pattern of
42 * "yyyy-MM-dd" is assumed. The following examples should clarify the point.
43 *
44 * <p>
45 * <table cellspacing="5px" border="1">
46 * <tr>
47 * <th><code>FileNamePattern</code> value</th>
48 * <th>Rollover schedule</th>
49 * <th>Example</th>
50 * </tr>
51 * <tr>
52 * <td nowrap="true"><code>/wombat/folder/foo.%d</code></td>
53 * <td>Daily rollover (at midnight). Due to the omission of the optional
54 * time and date pattern for the %d token specifier, the default pattern
55 * of "yyyy-MM-dd" is assumed, which corresponds to daily rollover.
56 * </td>
57 * <td>During November 23rd, 2004, logging output will go to
58 * the file <code>/wombat/foo.2004-11-23</code>. At midnight and for
59 * the rest of the 24th, logging output will be directed to
60 * <code>/wombat/foo.2004-11-24</code>.
61 * </td>
62 * </tr>
63 * <tr>
64 * <td nowrap="true"><code>/wombat/foo.%d{yyyy-MM}.log</code></td>
65 * <td>Rollover at the beginning of each month.</td>
66 * <td>During the month of October 2004, logging output will go to
67 * <code>/wombat/foo.2004-10.log</code>. After midnight of October 31st
68 * and for the rest of November, logging output will be directed to
69 * <code>/wombat/foo.2004-11.log</code>.
70 * </td>
71 * </tr>
72 * </table>
73 * <h2>Automatic file compression</h2>
74 * <code>TimeBasedRollingPolicy</code> supports automatic file compression.
75 * This feature is enabled if the value of the <b>FileNamePattern</b> option
76 * ends with <code>.gz</code> or <code>.zip</code>.
77 * <p>
78 * <table cellspacing="5px" border="1">
79 * <tr>
80 * <th><code>FileNamePattern</code> value</th>
81 * <th>Rollover schedule</th>
82 * <th>Example</th>
83 * </tr>
84 * <tr>
85 * <td nowrap="true"><code>/wombat/foo.%d.gz</code></td>
86 * <td>Daily rollover (at midnight) with automatic GZIP compression of the
87 * archived files.</td>
88 * <td>During November 23rd, 2004, logging output will go to
89 * the file <code>/wombat/foo.2004-11-23</code>. However, at midnight that
90 * file will be compressed to become <code>/wombat/foo.2004-11-23.gz</code>.
91 * For the 24th of November, logging output will be directed to
92 * <code>/wombat/folder/foo.2004-11-24</code> until its rolled over at the
93 * beginning of the next day.
94 * </td>
95 * </tr>
96 * </table>
97 *
98 * <h2>Decoupling the location of the active log file and the archived log files</h2>
99 * <p>The <em>active file</em> is defined as the log file for the current period
100 * whereas <em>archived files</em> are those files which have been rolled over
101 * in previous periods.
102 *
103 * <p>By setting the <b>ActiveFileName</b> option you can decouple the location
104 * of the active log file and the location of the archived log files.
105 * <p>
106 * <table cellspacing="5px" border="1">
107 * <tr>
108 * <th><code>FileNamePattern</code> value</th>
109 * <th>ActiveFileName</th>
110 * <th>Rollover schedule</th>
111 * <th>Example</th>
112 * </tr>
113 * <tr>
114 * <td nowrap="true"><code>/wombat/foo.log.%d</code></td>
115 * <td nowrap="true"><code>/wombat/foo.log</code></td>
116 * <td>Daily rollover.</td>
117 *
118 * <td>During November 23rd, 2004, logging output will go to
119 * the file <code>/wombat/foo.log</code>. However, at midnight that file
120 * will archived as <code>/wombat/foo.log.2004-11-23</code>. For the 24th
121 * of November, logging output will be directed to
122 * <code>/wombat/folder/foo.log</code> until its archived as
123 * <code>/wombat/foo.log.2004-11-24</code> at the beginning of the next
124 * day.
125 * </td>
126 * </tr>
127 * </table>
128 * <p>
129 * If configuring programatically, do not forget to call {@link #activateOptions}
130 * method before using this policy. Moreover, {@link #activateOptions} of
131 * <code> TimeBasedRollingPolicy</code> must be called <em>before</em> calling
132 * the {@link #activateOptions} method of the owning
133 * <code>RollingFileAppender</code>.
134 *
135 * @author Ceki Gülcü
136 * @author Curt Arnold
137 */
138 public final class TimeBasedRollingPolicy extends RollingPolicyBase
139 implements TriggeringPolicy {
140
141 /***
142 * Time for next determination if time for rollover.
143 */
144 private long nextCheck = 0;
145
146 /***
147 * File name at last rollover.
148 */
149 private String lastFileName = null;
150
151 /***
152 * Length of any file type suffix (.gz, .zip).
153 */
154 private int suffixLength = 0;
155
156 /***
157 * Constructs a new instance.
158 */
159 public TimeBasedRollingPolicy() {
160 }
161
162 /***
163 * Prepares instance of use.
164 */
165 public void activateOptions() {
166 super.activateOptions();
167
168 PatternConverter dtc = getDatePatternConverter();
169
170 if (dtc == null) {
171 throw new IllegalStateException(
172 "FileNamePattern [" + getFileNamePattern()
173 + "] does not contain a valid date format specifier");
174 }
175
176 long n = System.currentTimeMillis();
177 StringBuffer buf = new StringBuffer();
178 formatFileName(new Date(n), buf);
179 lastFileName = buf.toString();
180
181 suffixLength = 0;
182
183 if (lastFileName.endsWith(".gz")) {
184 suffixLength = 3;
185 } else if (lastFileName.endsWith(".zip")) {
186 suffixLength = 4;
187 }
188 }
189
190 /***
191 * {@inheritDoc}
192 */
193 public RolloverDescription initialize(
194 final String currentActiveFile, final boolean append) {
195 long n = System.currentTimeMillis();
196 nextCheck = ((n / 1000) + 1) * 1000;
197
198 StringBuffer buf = new StringBuffer();
199 formatFileName(new Date(n), buf);
200 lastFileName = buf.toString();
201
202
203
204
205
206 if (activeFileName != null) {
207 return new RolloverDescriptionImpl(activeFileName, append, null, null);
208 } else if (currentActiveFile != null) {
209 return new RolloverDescriptionImpl(
210 currentActiveFile, append, null, null);
211 } else {
212 return new RolloverDescriptionImpl(
213 lastFileName.substring(0, lastFileName.length() - suffixLength), append,
214 null, null);
215 }
216 }
217
218 /***
219 * {@inheritDoc}
220 */
221 public RolloverDescription rollover(final String currentActiveFile) {
222 long n = System.currentTimeMillis();
223 nextCheck = ((n / 1000) + 1) * 1000;
224
225 StringBuffer buf = new StringBuffer();
226 formatFileName(new Date(n), buf);
227
228 String newFileName = buf.toString();
229
230
231
232
233 if (newFileName.equals(lastFileName)) {
234 return null;
235 }
236
237 Action renameAction = null;
238 Action compressAction = null;
239 String lastBaseName =
240 lastFileName.substring(0, lastFileName.length() - suffixLength);
241 String nextActiveFile =
242 newFileName.substring(0, newFileName.length() - suffixLength);
243
244
245
246
247
248 if (!currentActiveFile.equals(lastBaseName)) {
249 renameAction =
250 new FileRenameAction(
251 new File(currentActiveFile), new File(lastBaseName), true);
252 nextActiveFile = currentActiveFile;
253 }
254
255 if (suffixLength == 3) {
256 compressAction =
257 new GZCompressAction(
258 new File(lastBaseName), new File(lastFileName), true);
259 }
260
261 if (suffixLength == 4) {
262 compressAction =
263 new ZipCompressAction(
264 new File(lastBaseName), new File(lastFileName), true);
265 }
266
267 lastFileName = newFileName;
268
269 return new RolloverDescriptionImpl(
270 nextActiveFile, false, renameAction, compressAction);
271 }
272
273 /***
274 * {@inheritDoc}
275 */
276 public boolean isTriggeringEvent(
277 final Appender appender, final LoggingEvent event, final String filename,
278 final long fileLength) {
279 return System.currentTimeMillis() >= nextCheck;
280 }
281 }