ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/core/loggers/FileLogger.java
Revision: 1.10
Committed: Sat May 18 18:16:01 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.9: +22 -3 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * Copyright (C) 2000-2002 i-scream
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 //---PACKAGE DECLARATION---
21 package uk.org.iscream.cms.server.core.loggers;
22
23 //---IMPORTS---
24 import uk.org.iscream.cms.server.util.*;
25 import uk.org.iscream.cms.server.core.*;
26 import java.util.Date;
27 import java.text.DateFormat;
28 import java.util.Locale;
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33
34 /**
35 * The FileLogger is an implementation of the LoggerImpl defined
36 * in the associated interface. It provides quite extensive features to allow
37 * logging ot a text file.
38 *
39 * @author $Author: tdb $
40 * @version $Id: FileLogger.java,v 1.9 2001/05/29 17:02:35 tdb Exp $
41 */
42 public class FileLogger implements LoggerImpl {
43
44 //---FINAL ATTRIBUTES---
45
46 /**
47 * The current CVS revision of this class
48 */
49 public final String REVISION = "$Revision: 1.9 $";
50
51 //---STATIC METHODS---
52
53 //---CONSTRUCTORS---
54
55 /**
56 * Creates a new FileLogger.
57 *
58 * The constructor checks that writing will be ok, then sets up the
59 * writer. It makes use of the fileCheck() method found later on to
60 * ensure writing will be ok, and throws an IOException if there is
61 * a problem. Errors at this stage cannot be logged because the
62 * logging mechanism has not be setup completely, so any problems
63 * are printed to the screen in the same format used for logging.
64 *
65 * @throws IOException if there is a problem with the file check.
66 */
67 public FileLogger() throws IOException{
68 filename = System.getProperty("uk.org.iscream.cms.server.LoggerClass.FileLogger.filename");
69 // Perform file check to make sure writing is ok
70 if(!fileCheck()){
71 // Have to system.out.println errors because logging mechanism failed !
72 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated"));
73 throw new IOException("File check failed, unable to create FileLog");
74 }
75 try{
76 // Setup the writer
77 writer = new BufferedWriter(new FileWriter(filename, true));
78 // File is now open
79 open = true;
80 }
81 catch(IOException e){
82 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to setup writer failed - " + e.getMessage()));
83 }
84 // Put an initial line into the log
85 write(toString(), Logger.SYSINIT, "started");
86 }
87
88 //---PUBLIC METHODS---
89
90 /**
91 * The write() method takes a line of text, pre-formatted
92 * and outputs it using a method defined by the actual
93 * implementation. The verbosity is given in case the
94 * implementation wishes to utilise it in the layout -
95 * eg. a different colour or font.
96 *
97 * This instance writes the line to a file.
98 *
99 * @param line A line of formatted text to be logged
100 * @param verbosity the verbosity of this message
101 */
102 public synchronized void write(String line, int verbosity) {
103 // Check to make sure file is open
104 if(open){
105 // Produce a nicely formatted line for the logfile
106 try{
107 // We have to synchronize here due to problems with two write()'s being called before a newLine()
108 synchronized(writer){
109 // Attempt to write the line
110 writer.write(line);
111 // Best to use newLine() as it will use the correct platform encoding
112 writer.newLine();
113 }
114 // Make sure the line is written immeidiately
115 writer.flush();
116 }
117 catch(IOException e){
118 // We'd best log the error
119 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Writing to logfile failed - " + e.getMessage()));
120 // As it's an IOException we should suspend logging
121 open = false;
122 }
123 }
124 else{
125 // If file is not open we should print this to the screen
126 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Write failed - file not open"));
127 }
128 }
129
130 /**
131 * The clear() method erases the contents of the logfile. It does
132 * this in a very clumsy way - by closing the writer and opening a
133 * new one which is set not to append. This results in the logfile
134 * being overwritten, which effectively erases it.
135 */
136 public void clear(){
137 // Check file is open
138 if(open){
139 write(toString(), Logger.SYSMSG, "Attempting to clear logfile");
140 try{
141 // Close appending writer
142 open = false;
143 writer.close();
144 // Open a non-appending writer
145 writer = new BufferedWriter(new FileWriter(filename));
146 open = true;
147 }
148 catch(IOException e){
149 // We'd best log the error
150 write(toString(), Logger.ERROR, "Attempt to clear logfile failed - "+e.getMessage());
151 // As it's an IOException we should suspend logging
152 open = false;
153 }
154 }
155 else{
156 // If file is not open we should print this to the screen
157 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Clearing failed - file not open"));
158 }
159 }
160
161 /**
162 * The close() method closes the logfile and no more data can be written.
163 * If writing is attempted after close() has been called an error will be
164 * generated. The open() method can be used to reopen or open a new file.
165 */
166 public void close(){
167 // Check to see if a file is open
168 if(open){
169 write(toString(), Logger.SYSMSG, "Attempting to terminate logging");
170 try{
171 open = false;
172 writer.close();
173 }
174 catch(IOException e){
175 write(toString(), Logger.ERROR, "Attempt to close logfile failed - " + e.getMessage());
176 // As it's an IOException we should suspend logging
177 open = false;
178 }
179 }
180 else{
181 // If file is not open we should print this to the screen
182 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Close failed - file not open"));
183 }
184 }
185
186 /**
187 * The open() method opens a new file after the close() method has
188 * been called on the FileLog. This enables the user to easily change
189 * the file to which the log is being written. An error is generated
190 * if a file is already open.
191 *
192 * @param filename The new file to write to.
193 * @throws IOException if the file cannot be written to.
194 */
195 public void open(String filename) throws IOException{
196 // Check to see if a file is open
197 if(open){
198 // If file is already open then we should log that this went wrong
199 write(this.toString(), Logger.WARNING, "Open failed - a file is already open");
200 }
201 else{
202 this.filename = filename;
203 if(!fileCheck()) {
204 // Have to system.out.println errors because logging mechanism failed !
205 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated"));
206 throw new IOException("File check failed, unable to create FileLog");
207 }
208 try{
209 // Setup the writer
210 writer = new BufferedWriter(new FileWriter(filename, true));
211 // File is now open
212 open = true;
213 }
214 catch(IOException e){
215 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to open writer failed - " + e.getMessage()));
216 }
217 }
218 }
219
220 /**
221 * The suspend() method halts writing to the file by setting the open
222 * boolean to false, and thus blocking the write() method. A line is
223 * written to the logfile beforehand to note that writing has been
224 * suspended.
225 */
226 public void suspend(){
227 // Check to see if a file is open
228 if(open){
229 // Make a note of the fact that writing has been suspended
230 write(toString(), Logger.SYSMSG, "Writing suspended");
231 // Make sure writing not permitted
232 open=false;
233 }
234 else{
235 // If file is not open we should print this to the screen
236 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Suspend failed - file not open"));
237 }
238 }
239
240 /**
241 * The resume() method resumes writing to the file by setting the open
242 * boolean to true. A line is written to the logfile to signify this
243 * event occuring.
244 */
245 public void resume(){
246 // Check to see if a file is open
247 if(open){
248 // If file is open we should print this to the screen
249 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Resume failed - file open"));
250 }
251 else{
252 // Permit writing again
253 open=true;
254 // Make a note of the fact that writing has been resumed
255 write(toString(), Logger.SYSMSG, "Writing resumed");
256 }
257 }
258
259 /**
260 * Overrides the {@link java.lang.Object#toString() Object.toString()}
261 * method to provide clean logging (every class should have this).
262 *
263 * This uses the uk.org.iscream.cms.server.util.FormatName class
264 * to format the toString()
265 *
266 * @return the name of this class and its CVS revision
267 */
268 public String toString() {
269 return FormatName.getName(
270 _name,
271 getClass().getName(),
272 REVISION);
273 }
274
275 //---PRIVATE METHODS---
276
277 /**
278 * The fileCheck() method is used to ensure that writing is ok. It
279 * performs so basic checks to make sure that if the file exists
280 * it can be written to, and if not a new file is created. A boolean
281 * result is returned to signify success or failure.
282 *
283 * @return A boolean value signify whether the test was sucessful.
284 */
285 private boolean fileCheck(){
286 boolean fileOK = false;
287 try{
288 File file = new File(filename);
289
290 // File already exists
291 if(file.exists()) {
292 /* Only want success if it is a file (not a directory)
293 * and can be written to.
294 */
295 if(file.isFile() && file.canWrite()) {
296 fileOK = true;
297 }
298 }
299 // Create a new file and suceed
300 else{
301 file.createNewFile();
302 fileOK = true;
303 }
304 }
305 catch(IOException e){
306 System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed - "+e.getMessage()));
307 }
308
309 return fileOK;
310 }
311
312 /**
313 * This method is provided if this class wishes to log
314 * a message itself.
315 *
316 * @param source A String representation of the source
317 * @param verbosity the verbosity of this message
318 * @param message The message to log
319 */
320 private void write(String source, int verbosity, String message) {
321 write(FormatName.formatLogLine(source, verbosity, message), verbosity);
322 }
323
324 //---ACCESSOR/MUTATOR METHODS---
325
326 //---ATTRIBUTES---
327
328 /**
329 * The filename of the currently open, or last open, file.
330 */
331 private String filename;
332
333 /**
334 * A reference to the writer being used.
335 */
336 private BufferedWriter writer;
337
338 /**
339 * A boolean signifying whether a file is open or not.
340 */
341 private boolean open = false;
342
343 /**
344 * This is the friendly identifier of the
345 * component this class is running in.
346 * eg, a Filter may be called "filter1",
347 * If this class does not have an owning
348 * component, a name from the configuration
349 * can be placed here. This name could also
350 * be changed to null for utility classes.
351 */
352 private String _name = Core.NAME;
353
354 //---STATIC ATTRIBUTES---
355
356 }