1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.core.loggers; |
2 |
> |
package uk.org.iscream.core.loggers; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.core.LoggerImpl; |
6 |
< |
import uk.ac.ukc.iscream.core.Logger; |
5 |
> |
import uk.org.iscream.util.*; |
6 |
> |
import uk.org.iscream.core.*; |
7 |
|
import java.util.Date; |
8 |
|
import java.text.DateFormat; |
9 |
|
import java.util.Locale; |
20 |
|
* @author $Author$ |
21 |
|
* @version $Id$ |
22 |
|
*/ |
23 |
< |
class FileLogger implements LoggerImpl { |
23 |
> |
public class FileLogger implements LoggerImpl { |
24 |
|
|
25 |
|
//---FINAL ATTRIBUTES--- |
26 |
|
|
46 |
|
* @throws IOException if there is a problem with the file check. |
47 |
|
*/ |
48 |
|
public FileLogger() throws IOException{ |
49 |
< |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
50 |
< |
filename = System.getProperty("uk.ac.ukc.iscream.LoggerClassParam1"); |
49 |
> |
filename = System.getProperty("uk.org.iscream.LoggerClass.FileLogger.filename"); |
50 |
|
// Perform file check to make sure writing is ok |
51 |
|
if(!fileCheck()){ |
52 |
|
// Have to system.out.println errors because logging mechanism failed ! |
53 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed, construction terminated")); |
53 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated")); |
54 |
|
throw new IOException("File check failed, unable to create FileLog"); |
55 |
|
} |
56 |
|
try{ |
60 |
|
open = true; |
61 |
|
} |
62 |
|
catch(IOException e){ |
63 |
< |
System.out.println(formatLogLine(this.toString(), "Attemp to setup writer failed: "+e.getMessage())); |
63 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to setup writer failed - " + e.getMessage())); |
64 |
|
} |
65 |
|
// Put an initial line into the log |
66 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
68 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
66 |
> |
write(toString(), Logger.SYSINIT, "started"); |
67 |
|
} |
68 |
|
|
69 |
|
//---PUBLIC METHODS--- |
70 |
|
|
71 |
< |
/** |
72 |
< |
* The write() method writes a line of log to the file, prepending |
73 |
< |
* it with some information about where to line came from and the |
74 |
< |
* date. This formatting is handled by the formatLogLine() method. |
75 |
< |
* There is some synchronisation here due to problems with the logfiles |
76 |
< |
* having multiple lines written on the same line, followed by dotted |
79 |
< |
* newlines in the wrong places. This is due to this method being |
80 |
< |
* called by multiple threads. |
71 |
> |
/** |
72 |
> |
* The write() method takes a line of text, pre-formatted |
73 |
> |
* and outputs it using a method defined by the actual |
74 |
> |
* implementation. The verbosity is given in case the |
75 |
> |
* implementation wishes to utilise it in the layout - |
76 |
> |
* eg. a different colour or font. |
77 |
|
* |
78 |
< |
* @param source A string representation of the calling object. |
79 |
< |
* @param message The message to be logged. |
80 |
< |
*/ |
81 |
< |
public synchronized void write(String source, int verbosity, String message) { |
78 |
> |
* This instance writes the line to a file. |
79 |
> |
* |
80 |
> |
* @param line A line of formatted text to be logged |
81 |
> |
* @param verbosity the verbosity of this message |
82 |
> |
*/ |
83 |
> |
public synchronized void write(String line, int verbosity) { |
84 |
|
// Check to make sure file is open |
85 |
|
if(open){ |
86 |
|
// Produce a nicely formatted line for the logfile |
89 |
– |
String line = formatLogLine(source, message); |
87 |
|
try{ |
88 |
|
// We have to synchronize here due to problems with two write()'s being called before a newLine() |
89 |
|
synchronized(writer){ |
97 |
|
} |
98 |
|
catch(IOException e){ |
99 |
|
// We'd best log the error |
100 |
< |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed: "+e.getMessage())); |
100 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Writing to logfile failed - " + e.getMessage())); |
101 |
|
// As it's an IOException we should suspend logging |
102 |
|
open = false; |
103 |
|
} |
104 |
|
} |
105 |
|
else{ |
106 |
|
// If file is not open we should print this to the screen |
107 |
< |
System.out.println(formatLogLine(this.toString(), "Write failed: file not open")); |
107 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Write failed - file not open")); |
108 |
|
} |
109 |
|
} |
110 |
|
|
117 |
|
public void clear(){ |
118 |
|
// Check file is open |
119 |
|
if(open){ |
120 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
120 |
> |
write(toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
121 |
|
try{ |
122 |
|
// Close appending writer |
123 |
|
open = false; |
128 |
|
} |
129 |
|
catch(IOException e){ |
130 |
|
// We'd best log the error |
131 |
< |
write(this.toString(), Logger.ERROR, "Attempt to clear logfile failed: "+e.getMessage()); |
131 |
> |
write(toString(), Logger.ERROR, "Attempt to clear logfile failed - "+e.getMessage()); |
132 |
|
// As it's an IOException we should suspend logging |
133 |
|
open = false; |
134 |
|
} |
135 |
|
} |
136 |
|
else{ |
137 |
|
// If file is not open we should print this to the screen |
138 |
< |
System.out.println(formatLogLine(this.toString(), "Clearing failed: file not open")); |
138 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Clearing failed - file not open")); |
139 |
|
} |
140 |
|
} |
141 |
|
|
147 |
|
public void close(){ |
148 |
|
// Check to see if a file is open |
149 |
|
if(open){ |
150 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
150 |
> |
write(toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
151 |
|
try{ |
152 |
|
open = false; |
153 |
|
writer.close(); |
154 |
|
} |
155 |
|
catch(IOException e){ |
156 |
< |
write(this.toString(), Logger.ERROR, "Attempt to close logfile failed: "+e.getMessage()); |
156 |
> |
write(toString(), Logger.ERROR, "Attempt to close logfile failed - " + e.getMessage()); |
157 |
|
// As it's an IOException we should suspend logging |
158 |
|
open = false; |
159 |
|
} |
160 |
|
} |
161 |
|
else{ |
162 |
|
// If file is not open we should print this to the screen |
163 |
< |
System.out.println(formatLogLine(this.toString(), "Close failed: file not open")); |
163 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Close failed - file not open")); |
164 |
|
} |
165 |
|
} |
166 |
|
|
177 |
|
// Check to see if a file is open |
178 |
|
if(open){ |
179 |
|
// If file is already open then we should log that this went wrong |
180 |
< |
write(this.toString(), Logger.WARNING, "Open failed: a file is already open"); |
180 |
> |
write(this.toString(), Logger.WARNING, "Open failed - a file is already open"); |
181 |
|
} |
182 |
|
else{ |
183 |
|
this.filename = filename; |
184 |
|
if(!fileCheck()) { |
185 |
|
// Have to system.out.println errors because logging mechanism failed ! |
186 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed, construction terminated")); |
186 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated")); |
187 |
|
throw new IOException("File check failed, unable to create FileLog"); |
188 |
|
} |
189 |
|
try{ |
193 |
|
open = true; |
194 |
|
} |
195 |
|
catch(IOException e){ |
196 |
< |
System.out.println(formatLogLine(this.toString(), "Attempt to open writer failed: "+e.getMessage())); |
196 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to open writer failed - " + e.getMessage())); |
197 |
|
} |
198 |
|
} |
199 |
|
} |
208 |
|
// Check to see if a file is open |
209 |
|
if(open){ |
210 |
|
// Make a note of the fact that writing has been suspended |
211 |
< |
write(this.toString(), Logger.SYSMSG, "Writing suspended"); |
211 |
> |
write(toString(), Logger.SYSMSG, "Writing suspended"); |
212 |
|
// Make sure writing not permitted |
213 |
|
open=false; |
214 |
|
} |
215 |
|
else{ |
216 |
|
// If file is not open we should print this to the screen |
217 |
< |
System.out.println(formatLogLine(this.toString(), "Suspend failed: file not open")); |
217 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Suspend failed - file not open")); |
218 |
|
} |
219 |
|
} |
220 |
|
|
227 |
|
// Check to see if a file is open |
228 |
|
if(open){ |
229 |
|
// If file is open we should print this to the screen |
230 |
< |
System.out.println(formatLogLine(this.toString(), "Resume failed: file open")); |
230 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Resume failed - file open")); |
231 |
|
} |
232 |
|
else{ |
233 |
|
// Permit writing again |
234 |
|
open=true; |
235 |
|
// Make a note of the fact that writing has been resumed |
236 |
< |
write(this.toString(), Logger.SYSMSG, "Writing resumed"); |
236 |
> |
write(toString(), Logger.SYSMSG, "Writing resumed"); |
237 |
|
} |
238 |
|
} |
239 |
|
|
241 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
242 |
|
* method to provide clean logging (every class should have this). |
243 |
|
* |
244 |
+ |
* This uses the uk.org.iscream.util.FormatName class |
245 |
+ |
* to format the toString() |
246 |
+ |
* |
247 |
|
* @return the name of this class and its CVS revision |
248 |
|
*/ |
249 |
|
public String toString() { |
250 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
250 |
> |
return FormatName.getName( |
251 |
> |
_name, |
252 |
> |
getClass().getName(), |
253 |
> |
REVISION); |
254 |
|
} |
255 |
|
|
256 |
|
//---PRIVATE METHODS--- |
257 |
|
|
258 |
|
/** |
256 |
– |
* This method generates a nicely formatted line for the log, |
257 |
– |
* including the date/time and the source of the message. The date |
258 |
– |
* and time are formatted using the DateFormat class, and the source |
259 |
– |
* class is formatted using the toString() method found in every |
260 |
– |
* source file. This is then prepended to the message and returned. |
261 |
– |
* |
262 |
– |
* @param source A string representation of the calling object. |
263 |
– |
* @param message The message to be logged. |
264 |
– |
* @return The string to be written to the log. |
265 |
– |
*/ |
266 |
– |
private String formatLogLine(String source, String message){ |
267 |
– |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
268 |
– |
return "[" + date + "] " + source + ": " + message; |
269 |
– |
} |
270 |
– |
|
271 |
– |
/** |
259 |
|
* The fileCheck() method is used to ensure that writing is ok. It |
260 |
|
* performs so basic checks to make sure that if the file exists |
261 |
|
* it can be written to, and if not a new file is created. A boolean |
284 |
|
} |
285 |
|
} |
286 |
|
catch(IOException e){ |
287 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed: "+e.getMessage())); |
287 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed - "+e.getMessage())); |
288 |
|
} |
289 |
|
|
290 |
|
return fileOK; |
291 |
|
} |
292 |
|
|
293 |
+ |
/** |
294 |
+ |
* This method is provided if this class wishes to log |
295 |
+ |
* a message itself. |
296 |
+ |
* |
297 |
+ |
* @param source A String representation of the source |
298 |
+ |
* @param verbosity the verbosity of this message |
299 |
+ |
* @param message The message to log |
300 |
+ |
*/ |
301 |
+ |
private void write(String source, int verbosity, String message) { |
302 |
+ |
write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
303 |
+ |
} |
304 |
+ |
|
305 |
|
//---ACCESSOR/MUTATOR METHODS--- |
306 |
|
|
307 |
|
//---ATTRIBUTES--- |
311 |
|
*/ |
312 |
|
private String filename; |
313 |
|
|
315 |
– |
/** |
316 |
– |
* The verbosity level of this instance |
317 |
– |
*/ |
318 |
– |
private int _verbosityLevel; |
319 |
– |
|
314 |
|
/** |
315 |
|
* A reference to the writer being used. |
316 |
|
*/ |
320 |
|
* A boolean signifying whether a file is open or not. |
321 |
|
*/ |
322 |
|
private boolean open = false; |
323 |
+ |
|
324 |
+ |
/** |
325 |
+ |
* This is the friendly identifier of the |
326 |
+ |
* component this class is running in. |
327 |
+ |
* eg, a Filter may be called "filter1", |
328 |
+ |
* If this class does not have an owning |
329 |
+ |
* component, a name from the configuration |
330 |
+ |
* can be placed here. This name could also |
331 |
+ |
* be changed to null for utility classes. |
332 |
+ |
*/ |
333 |
+ |
private String _name = Core.NAME; |
334 |
|
|
335 |
|
//---STATIC ATTRIBUTES--- |
336 |
|
|