KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > logger > FileLogger


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.logger;
30
31
32 import java.io.File JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.sql.Timestamp JavaDoc;
37 import org.apache.catalina.LifecycleException;
38 import org.apache.catalina.util.LifecycleSupport;
39 import org.apache.catalina.util.StringManager;
40
41
42 /**
43  * Implementation of <b>Logger</b> that appends log messages to a file
44  * named {prefix}.{date}.{suffix} in a configured directory, with an
45  * optional preceding timestamp.
46  *
47  * @author Craig R. McClanahan
48  * @version $Revision: 1.2 $ $Date: 2005/12/08 01:27:45 $
49  */

50
51 public class FileLogger
52     extends LoggerBase {
53
54
55     // ----------------------------------------------------- Instance Variables
56

57
58     /**
59      * The as-of date for the currently open log file, or a zero-length
60      * string if there is no open log file.
61      */

62     private String JavaDoc date = "";
63
64
65     /**
66      * The directory in which log files are created.
67      */

68     private String JavaDoc directory = "logs";
69
70
71     /**
72      * The descriptive information about this implementation.
73      */

74     protected static final String JavaDoc info =
75         "org.apache.catalina.logger.FileLogger/1.0";
76
77
78     /**
79      * The prefix that is added to log file filenames.
80      */

81     private String JavaDoc prefix = "catalina.";
82
83
84     /**
85      * The string manager for this package.
86      */

87     private StringManager sm =
88         StringManager.getManager(Constants.Package);
89
90
91     /**
92      * Has this component been started?
93      */

94     private boolean started = false;
95
96
97     /**
98      * The suffix that is added to log file filenames.
99      */

100     private String JavaDoc suffix = ".log";
101
102
103     /**
104      * Should logged messages be date/time stamped?
105      */

106     private boolean timestamp = false;
107
108
109     /**
110      * The PrintWriter to which we are currently logging, if any.
111      */

112     private PrintWriter JavaDoc writer = null;
113
114
115     // ------------------------------------------------------------- Properties
116

117
118     /**
119      * Return the directory in which we create log files.
120      */

121     public String JavaDoc getDirectory() {
122
123         return (directory);
124
125     }
126
127
128     /**
129      * Set the directory in which we create log files.
130      *
131      * @param directory The new log file directory
132      */

133     public void setDirectory(String JavaDoc directory) {
134
135         String JavaDoc oldDirectory = this.directory;
136         this.directory = directory;
137         support.firePropertyChange("directory", oldDirectory, this.directory);
138
139     }
140
141
142     /**
143      * Return the log file prefix.
144      */

145     public String JavaDoc getPrefix() {
146
147         return (prefix);
148
149     }
150
151
152     /**
153      * Set the log file prefix.
154      *
155      * @param prefix The new log file prefix
156      */

157     public void setPrefix(String JavaDoc prefix) {
158
159         String JavaDoc oldPrefix = this.prefix;
160         this.prefix = prefix;
161         support.firePropertyChange("prefix", oldPrefix, this.prefix);
162
163     }
164
165
166     /**
167      * Return the log file suffix.
168      */

169     public String JavaDoc getSuffix() {
170
171         return (suffix);
172
173     }
174
175
176     /**
177      * Set the log file suffix.
178      *
179      * @param suffix The new log file suffix
180      */

181     public void setSuffix(String JavaDoc suffix) {
182
183         String JavaDoc oldSuffix = this.suffix;
184         this.suffix = suffix;
185         support.firePropertyChange("suffix", oldSuffix, this.suffix);
186
187     }
188
189
190     /**
191      * Return the timestamp flag.
192      */

193     public boolean getTimestamp() {
194
195         return (timestamp);
196
197     }
198
199
200     /**
201      * Set the timestamp flag.
202      *
203      * @param timestamp The new timestamp flag
204      */

205     public void setTimestamp(boolean timestamp) {
206
207         boolean oldTimestamp = this.timestamp;
208         this.timestamp = timestamp;
209         support.firePropertyChange("timestamp", new Boolean JavaDoc(oldTimestamp),
210                                    new Boolean JavaDoc(this.timestamp));
211
212     }
213
214
215     // --------------------------------------------------------- Public Methods
216

217
218     /**
219      * Writes the specified message to a servlet log file, usually an event
220      * log. The name and type of the servlet log is specific to the
221      * servlet container.
222      *
223      * @param msg A <code>String</code> specifying the message to be written
224      * to the log file
225      */

226     public void log(String JavaDoc msg) {
227
228         // Construct the timestamp we will use, if requested
229
Timestamp JavaDoc ts = new Timestamp JavaDoc(System.currentTimeMillis());
230         String JavaDoc tsString = ts.toString().substring(0, 19);
231         String JavaDoc tsDate = tsString.substring(0, 10);
232
233         // If the date has changed, switch log files
234
if (!date.equals(tsDate)) {
235             synchronized (this) {
236                 if (!date.equals(tsDate)) {
237                     close();
238                     date = tsDate;
239                     open();
240                 }
241             }
242         }
243
244         // Log this message, timestamped if necessary
245
if (writer != null) {
246             if (timestamp) {
247                 writer.println(tsString + " " + msg);
248             } else {
249                 writer.println(msg);
250             }
251         }
252
253     }
254
255
256     // -------------------------------------------------------- Private Methods
257

258
259     /**
260      * Close the currently open log file (if any)
261      */

262     private void close() {
263
264         if (writer == null)
265             return;
266         writer.flush();
267         writer.close();
268         writer = null;
269         date = "";
270
271     }
272
273
274     /**
275      * Open the new log file for the date specified by <code>date</code>.
276      */

277     private void open() {
278
279         // Create the directory if necessary
280
File JavaDoc dir = new File JavaDoc(directory);
281         if (!dir.isAbsolute())
282             dir = new File JavaDoc(System.getProperty("catalina.base"), directory);
283         dir.mkdirs();
284
285         // Open the current log file
286
try {
287             String JavaDoc pathname = dir.getAbsolutePath() + File.separator +
288                 prefix + date + suffix;
289             writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(pathname, true), true);
290         } catch (IOException JavaDoc e) {
291             writer = null;
292         }
293
294     }
295
296
297     // ------------------------------------------------------ Lifecycle Methods
298

299
300     /**
301      * Prepare for the beginning of active use of the public methods of this
302      * component. This method should be called after <code>configure()</code>,
303      * and before any of the public methods of the component are utilized.
304      *
305      * @exception LifecycleException if this component detects a fatal error
306      * that prevents this component from being used
307      */

308     public void start() throws LifecycleException {
309
310         // Validate and update our current component state
311
if (started)
312             throw new LifecycleException
313                 (sm.getString("fileLogger.alreadyStarted"));
314         lifecycle.fireLifecycleEvent(START_EVENT, null);
315         started = true;
316         
317         super.start();
318
319     }
320
321
322     /**
323      * Gracefully terminate the active use of the public methods of this
324      * component. This method should be the last one called on a given
325      * instance of this component.
326      *
327      * @exception LifecycleException if this component detects a fatal error
328      * that needs to be reported
329      */

330     public void stop() throws LifecycleException {
331
332         // Validate and update our current component state
333
if (!started)
334             throw new LifecycleException
335                 (sm.getString("fileLogger.notStarted"));
336         lifecycle.fireLifecycleEvent(STOP_EVENT, null);
337         started = false;
338
339         close();
340         
341         super.stop();
342
343     }
344
345
346 }
347
Popular Tags