1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | #if !defined(_LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H)
|
---|
20 | #define _LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H
|
---|
21 |
|
---|
22 | #include <log4cxx/portability.h>
|
---|
23 | #include <log4cxx/rolling/rollingpolicybase.h>
|
---|
24 | #include <log4cxx/rolling/triggeringpolicy.h>
|
---|
25 |
|
---|
26 | namespace log4cxx {
|
---|
27 |
|
---|
28 | namespace rolling {
|
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite
|
---|
34 | * powerful.
|
---|
35 | *
|
---|
36 | * <p>In order to use <code>TimeBasedRollingPolicy</code>, the
|
---|
37 | * <b>FileNamePattern</b> option must be set. It basically specifies the name of the
|
---|
38 | * rolled log files. The value <code>FileNamePattern</code> should consist of
|
---|
39 | * the name of the file, plus a suitably placed <code>%d</code> conversion
|
---|
40 | * specifier. The <code>%d</code> conversion specifier may contain a date and
|
---|
41 | * time pattern as specified by the {@link log4cxx::helpers::SimpleDateFormat} class. If
|
---|
42 | * the date and time pattern is ommitted, then the default pattern of
|
---|
43 | * "yyyy-MM-dd" is assumed. The following examples should clarify the point.
|
---|
44 | *
|
---|
45 | * <p>
|
---|
46 | * <table cellspacing="5px" border="1">
|
---|
47 | * <tr>
|
---|
48 | * <th><code>FileNamePattern</code> value</th>
|
---|
49 | * <th>Rollover schedule</th>
|
---|
50 | * <th>Example</th>
|
---|
51 | * </tr>
|
---|
52 | * <tr>
|
---|
53 | * <td nowrap="true"><code>/wombat/folder/foo.%d</code></td>
|
---|
54 | * <td>Daily rollover (at midnight). Due to the omission of the optional
|
---|
55 | * time and date pattern for the %d token specifier, the default pattern
|
---|
56 | * of "yyyy-MM-dd" is assumed, which corresponds to daily rollover.
|
---|
57 | * </td>
|
---|
58 | * <td>During November 23rd, 2004, logging output will go to
|
---|
59 | * the file <code>/wombat/foo.2004-11-23</code>. At midnight and for
|
---|
60 | * the rest of the 24th, logging output will be directed to
|
---|
61 | * <code>/wombat/foo.2004-11-24</code>.
|
---|
62 | * </td>
|
---|
63 | * </tr>
|
---|
64 | * <tr>
|
---|
65 | * <td nowrap="true"><code>/wombat/foo.%d{yyyy-MM}.log</code></td>
|
---|
66 | * <td>Rollover at the beginning of each month.</td>
|
---|
67 | * <td>During the month of October 2004, logging output will go to
|
---|
68 | * <code>/wombat/foo.2004-10.log</code>. After midnight of October 31st
|
---|
69 | * and for the rest of November, logging output will be directed to
|
---|
70 | * <code>/wombat/foo.2004-11.log</code>.
|
---|
71 | * </td>
|
---|
72 | * </tr>
|
---|
73 | * </table>
|
---|
74 | * <h2>Automatic file compression</h2>
|
---|
75 | * <code>TimeBasedRollingPolicy</code> supports automatic file compression.
|
---|
76 | * This feature is enabled if the value of the <b>FileNamePattern</b> option
|
---|
77 | * ends with <code>.gz</code> or <code>.zip</code>.
|
---|
78 | * <p>
|
---|
79 | * <table cellspacing="5px" border="1">
|
---|
80 | * <tr>
|
---|
81 | * <th><code>FileNamePattern</code> value</th>
|
---|
82 | * <th>Rollover schedule</th>
|
---|
83 | * <th>Example</th>
|
---|
84 | * </tr>
|
---|
85 | * <tr>
|
---|
86 | * <td nowrap="true"><code>/wombat/foo.%d.gz</code></td>
|
---|
87 | * <td>Daily rollover (at midnight) with automatic GZIP compression of the
|
---|
88 | * arcived files.</td>
|
---|
89 | * <td>During November 23rd, 2004, logging output will go to
|
---|
90 | * the file <code>/wombat/foo.2004-11-23</code>. However, at midnight that
|
---|
91 | * file will be compressed to become <code>/wombat/foo.2004-11-23.gz</code>.
|
---|
92 | * For the 24th of November, logging output will be directed to
|
---|
93 | * <code>/wombat/folder/foo.2004-11-24</code> until its rolled over at the
|
---|
94 | * beginning of the next day.
|
---|
95 | * </td>
|
---|
96 | * </tr>
|
---|
97 | * </table>
|
---|
98 | *
|
---|
99 | * <h2>Decoupling the location of the active log file and the archived log files</h2>
|
---|
100 | * <p>The <em>active file</em> is defined as the log file for the current period
|
---|
101 | * whereas <em>archived files</em> are thos files which have been rolled over
|
---|
102 | * in previous periods.
|
---|
103 | *
|
---|
104 | * <p>By setting the <b>ActiveFileName</b> option you can decouple the location
|
---|
105 | * of the active log file and the location of the archived log files.
|
---|
106 | * <p>
|
---|
107 | * <table cellspacing="5px" border="1">
|
---|
108 | * <tr>
|
---|
109 | * <th><code>FileNamePattern</code> value</th>
|
---|
110 | * <th>ActiveFileName</th>
|
---|
111 | * <th>Rollover schedule</th>
|
---|
112 | * <th>Example</th>
|
---|
113 | * </tr>
|
---|
114 | * <tr>
|
---|
115 | * <td nowrap="true"><code>/wombat/foo.log.%d</code></td>
|
---|
116 | * <td nowrap="true"><code>/wombat/foo.log</code></td>
|
---|
117 | * <td>Daily rollover.</td>
|
---|
118 | *
|
---|
119 | * <td>During November 23rd, 2004, logging output will go to
|
---|
120 | * the file <code>/wombat/foo.log</code>. However, at midnight that file
|
---|
121 | * will archived as <code>/wombat/foo.log.2004-11-23</code>. For the 24th
|
---|
122 | * of November, logging output will be directed to
|
---|
123 | * <code>/wombat/folder/foo.log</code> until its archived as
|
---|
124 | * <code>/wombat/foo.log.2004-11-24</code> at the beginning of the next
|
---|
125 | * day.
|
---|
126 | * </td>
|
---|
127 | * </tr>
|
---|
128 | * </table>
|
---|
129 | * <p>
|
---|
130 | * If configuring programatically, do not forget to call {@link #activateOptions}
|
---|
131 | * method before using this policy. Moreover, {@link #activateOptions} of
|
---|
132 | * <code> TimeBasedRollingPolicy</code> must be called <em>before</em> calling
|
---|
133 | * the {@link #activateOptions} method of the owning
|
---|
134 | * <code>RollingFileAppender</code>.
|
---|
135 | *
|
---|
136 | *
|
---|
137 | *
|
---|
138 | */
|
---|
139 | class LOG4CXX_EXPORT TimeBasedRollingPolicy : public RollingPolicyBase,
|
---|
140 | public TriggeringPolicy {
|
---|
141 | DECLARE_LOG4CXX_OBJECT(TimeBasedRollingPolicy)
|
---|
142 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
143 | LOG4CXX_CAST_ENTRY(TimeBasedRollingPolicy)
|
---|
144 | LOG4CXX_CAST_ENTRY_CHAIN(RollingPolicyBase)
|
---|
145 | LOG4CXX_CAST_ENTRY_CHAIN(TriggeringPolicy)
|
---|
146 | END_LOG4CXX_CAST_MAP()
|
---|
147 |
|
---|
148 | private:
|
---|
149 | /**
|
---|
150 | * Time for next determination if time for rollover.
|
---|
151 | */
|
---|
152 | log4cxx_time_t nextCheck;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * File name at last rollover.
|
---|
156 | */
|
---|
157 | LogString lastFileName;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Length of any file type suffix (.gz, .zip).
|
---|
161 | */
|
---|
162 | int suffixLength;
|
---|
163 |
|
---|
164 | public:
|
---|
165 | TimeBasedRollingPolicy();
|
---|
166 | void addRef() const;
|
---|
167 | void releaseRef() const;
|
---|
168 | void activateOptions(log4cxx::helpers::Pool& );
|
---|
169 | /**
|
---|
170 | * Initialize the policy and return any initial actions for rolling file appender.
|
---|
171 | *
|
---|
172 | * @param file current value of RollingFileAppender.getFile().
|
---|
173 | * @param append current value of RollingFileAppender.getAppend().
|
---|
174 | * @param pool pool for any required allocations.
|
---|
175 | * @return Description of the initialization, may be null to indicate
|
---|
176 | * no initialization needed.
|
---|
177 | * @throws SecurityException if denied access to log files.
|
---|
178 | */
|
---|
179 | RolloverDescriptionPtr initialize(
|
---|
180 | const LogString& file,
|
---|
181 | const bool append,
|
---|
182 | log4cxx::helpers::Pool& pool);
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Prepare for a rollover. This method is called prior to
|
---|
186 | * closing the active log file, performs any necessary
|
---|
187 | * preliminary actions and describes actions needed
|
---|
188 | * after close of current log file.
|
---|
189 | *
|
---|
190 | * @param activeFile file name for current active log file.
|
---|
191 | * @param pool pool for any required allocations.
|
---|
192 | * @return Description of pending rollover, may be null to indicate no rollover
|
---|
193 | * at this time.
|
---|
194 | * @throws SecurityException if denied access to log files.
|
---|
195 | */
|
---|
196 | RolloverDescriptionPtr rollover(const LogString& activeFile,
|
---|
197 | log4cxx::helpers::Pool& pool);
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Determines if a rollover may be appropriate at this time. If
|
---|
201 | * true is returned, RolloverPolicy.rollover will be called but it
|
---|
202 | * can determine that a rollover is not warranted.
|
---|
203 | *
|
---|
204 | * @param appender A reference to the appender.
|
---|
205 | * @param event A reference to the currently event.
|
---|
206 | * @param filename The filename for the currently active log file.
|
---|
207 | * @param fileLength Length of the file in bytes.
|
---|
208 | * @return true if a rollover should occur.
|
---|
209 | */
|
---|
210 | virtual bool isTriggeringEvent(
|
---|
211 | Appender* appender,
|
---|
212 | const log4cxx::spi::LoggingEventPtr& event,
|
---|
213 | const LogString& filename,
|
---|
214 | size_t fileLength);
|
---|
215 |
|
---|
216 | protected:
|
---|
217 | log4cxx::pattern::PatternMap getFormatSpecifiers() const;
|
---|
218 |
|
---|
219 | };
|
---|
220 |
|
---|
221 | LOG4CXX_PTR_DEF(TimeBasedRollingPolicy);
|
---|
222 |
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endif
|
---|
227 |
|
---|