2 |
|
package uk.ac.ukc.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.ac.ukc.iscream.util.*; |
6 |
> |
import uk.ac.ukc.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 |
|
|
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"); |
50 |
> |
filename = System.getProperty("uk.ac.ukc.iscream.LoggerClass.FileLogger.filename"); |
51 |
|
// Perform file check to make sure writing is ok |
52 |
|
if(!fileCheck()){ |
53 |
|
// Have to system.out.println errors because logging mechanism failed ! |
61 |
|
open = true; |
62 |
|
} |
63 |
|
catch(IOException e){ |
64 |
< |
System.out.println(formatLogLine(this.toString(), "Attemp to setup writer failed: "+e.getMessage())); |
64 |
> |
System.out.println(formatLogLine(toString(), "Attempt to setup writer failed - " + e.getMessage())); |
65 |
|
} |
66 |
|
// Put an initial line into the log |
67 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
68 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
67 |
> |
write(toString(), Logger.SYSINIT, "started"); |
68 |
> |
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
69 |
|
} |
70 |
|
|
71 |
|
//---PUBLIC METHODS--- |
83 |
|
* @param message The message to be logged. |
84 |
|
*/ |
85 |
|
public synchronized void write(String source, int verbosity, String message) { |
86 |
< |
// Check to make sure file is open |
87 |
< |
if(open){ |
88 |
< |
// Produce a nicely formatted line for the logfile |
89 |
< |
String line = formatLogLine(source, message); |
90 |
< |
try{ |
91 |
< |
// We have to synchronize here due to problems with two write()'s being called before a newLine() |
92 |
< |
synchronized(writer){ |
93 |
< |
// Attempt to write the line |
94 |
< |
writer.write(line); |
95 |
< |
// Best to use newLine() as it will use the correct platform encoding |
96 |
< |
writer.newLine(); |
97 |
< |
} |
98 |
< |
// Make sure the line is written immeidiately |
99 |
< |
writer.flush(); |
100 |
< |
} |
101 |
< |
catch(IOException e){ |
102 |
< |
// We'd best log the error |
103 |
< |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed: "+e.getMessage())); |
104 |
< |
// As it's an IOException we should suspend logging |
105 |
< |
open = false; |
106 |
< |
} |
107 |
< |
} |
108 |
< |
else{ |
109 |
< |
// If file is not open we should print this to the screen |
110 |
< |
System.out.println(formatLogLine(this.toString(), "Write failed: file not open")); |
111 |
< |
} |
86 |
> |
if (verbosity <= _verbosityLevel) { |
87 |
> |
// Check to make sure file is open |
88 |
> |
if(open){ |
89 |
> |
// Produce a nicely formatted line for the logfile |
90 |
> |
String line = formatLogLine(source, message); |
91 |
> |
try{ |
92 |
> |
// We have to synchronize here due to problems with two write()'s being called before a newLine() |
93 |
> |
synchronized(writer){ |
94 |
> |
// Attempt to write the line |
95 |
> |
writer.write(line); |
96 |
> |
// Best to use newLine() as it will use the correct platform encoding |
97 |
> |
writer.newLine(); |
98 |
> |
} |
99 |
> |
// Make sure the line is written immeidiately |
100 |
> |
writer.flush(); |
101 |
> |
} |
102 |
> |
catch(IOException e){ |
103 |
> |
// We'd best log the error |
104 |
> |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed - " + e.getMessage())); |
105 |
> |
// As it's an IOException we should suspend logging |
106 |
> |
open = false; |
107 |
> |
} |
108 |
> |
} |
109 |
> |
else{ |
110 |
> |
// If file is not open we should print this to the screen |
111 |
> |
System.out.println(formatLogLine(this.toString(), "Write failed - file not open")); |
112 |
> |
} |
113 |
> |
} |
114 |
|
} |
115 |
|
|
116 |
|
/** |
122 |
|
public void clear(){ |
123 |
|
// Check file is open |
124 |
|
if(open){ |
125 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
125 |
> |
write(toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
126 |
|
try{ |
127 |
|
// Close appending writer |
128 |
|
open = false; |
133 |
|
} |
134 |
|
catch(IOException e){ |
135 |
|
// We'd best log the error |
136 |
< |
write(this.toString(), Logger.ERROR, "Attempt to clear logfile failed: "+e.getMessage()); |
136 |
> |
write(toString(), Logger.ERROR, "Attempt to clear logfile failed - "+e.getMessage()); |
137 |
|
// As it's an IOException we should suspend logging |
138 |
|
open = false; |
139 |
|
} |
140 |
|
} |
141 |
|
else{ |
142 |
|
// If file is not open we should print this to the screen |
143 |
< |
System.out.println(formatLogLine(this.toString(), "Clearing failed: file not open")); |
143 |
> |
System.out.println(formatLogLine(toString(), "Clearing failed - file not open")); |
144 |
|
} |
145 |
|
} |
146 |
|
|
152 |
|
public void close(){ |
153 |
|
// Check to see if a file is open |
154 |
|
if(open){ |
155 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
155 |
> |
write(toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
156 |
|
try{ |
157 |
|
open = false; |
158 |
|
writer.close(); |
159 |
|
} |
160 |
|
catch(IOException e){ |
161 |
< |
write(this.toString(), Logger.ERROR, "Attempt to close logfile failed: "+e.getMessage()); |
161 |
> |
write(toString(), Logger.ERROR, "Attempt to close logfile failed - " + e.getMessage()); |
162 |
|
// As it's an IOException we should suspend logging |
163 |
|
open = false; |
164 |
|
} |
165 |
|
} |
166 |
|
else{ |
167 |
|
// If file is not open we should print this to the screen |
168 |
< |
System.out.println(formatLogLine(this.toString(), "Close failed: file not open")); |
168 |
> |
System.out.println(formatLogLine(toString(), "Close failed - file not open")); |
169 |
|
} |
170 |
|
} |
171 |
|
|
182 |
|
// Check to see if a file is open |
183 |
|
if(open){ |
184 |
|
// If file is already open then we should log that this went wrong |
185 |
< |
write(this.toString(), Logger.WARNING, "Open failed: a file is already open"); |
185 |
> |
write(this.toString(), Logger.WARNING, "Open failed - a file is already open"); |
186 |
|
} |
187 |
|
else{ |
188 |
|
this.filename = filename; |
189 |
|
if(!fileCheck()) { |
190 |
|
// Have to system.out.println errors because logging mechanism failed ! |
191 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed, construction terminated")); |
191 |
> |
System.out.println(formatLogLine(toString(), "File check failed, construction terminated")); |
192 |
|
throw new IOException("File check failed, unable to create FileLog"); |
193 |
|
} |
194 |
|
try{ |
198 |
|
open = true; |
199 |
|
} |
200 |
|
catch(IOException e){ |
201 |
< |
System.out.println(formatLogLine(this.toString(), "Attempt to open writer failed: "+e.getMessage())); |
201 |
> |
System.out.println(formatLogLine(toString(), "Attempt to open writer failed - " + e.getMessage())); |
202 |
|
} |
203 |
|
} |
204 |
|
} |
213 |
|
// Check to see if a file is open |
214 |
|
if(open){ |
215 |
|
// Make a note of the fact that writing has been suspended |
216 |
< |
write(this.toString(), Logger.SYSMSG, "Writing suspended"); |
216 |
> |
write(toString(), Logger.SYSMSG, "Writing suspended"); |
217 |
|
// Make sure writing not permitted |
218 |
|
open=false; |
219 |
|
} |
220 |
|
else{ |
221 |
|
// If file is not open we should print this to the screen |
222 |
< |
System.out.println(formatLogLine(this.toString(), "Suspend failed: file not open")); |
222 |
> |
System.out.println(formatLogLine(toString(), "Suspend failed - file not open")); |
223 |
|
} |
224 |
|
} |
225 |
|
|
232 |
|
// Check to see if a file is open |
233 |
|
if(open){ |
234 |
|
// If file is open we should print this to the screen |
235 |
< |
System.out.println(formatLogLine(this.toString(), "Resume failed: file open")); |
235 |
> |
System.out.println(formatLogLine(toString(), "Resume failed - file open")); |
236 |
|
} |
237 |
|
else{ |
238 |
|
// Permit writing again |
239 |
|
open=true; |
240 |
|
// Make a note of the fact that writing has been resumed |
241 |
< |
write(this.toString(), Logger.SYSMSG, "Writing resumed"); |
241 |
> |
write(toString(), Logger.SYSMSG, "Writing resumed"); |
242 |
|
} |
243 |
|
} |
244 |
|
|
246 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
247 |
|
* method to provide clean logging (every class should have this). |
248 |
|
* |
249 |
+ |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
250 |
+ |
* to format the toString() |
251 |
+ |
* |
252 |
|
* @return the name of this class and its CVS revision |
253 |
|
*/ |
254 |
|
public String toString() { |
255 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
255 |
> |
return FormatName.getName( |
256 |
> |
_name, |
257 |
> |
getClass().getName(), |
258 |
> |
REVISION); |
259 |
|
} |
260 |
|
|
261 |
|
//---PRIVATE METHODS--- |
305 |
|
} |
306 |
|
} |
307 |
|
catch(IOException e){ |
308 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed: "+e.getMessage())); |
308 |
> |
System.out.println(formatLogLine(toString(), "File check failed - "+e.getMessage())); |
309 |
|
} |
310 |
|
|
311 |
|
return fileOK; |
334 |
|
* A boolean signifying whether a file is open or not. |
335 |
|
*/ |
336 |
|
private boolean open = false; |
337 |
+ |
|
338 |
+ |
/** |
339 |
+ |
* This is the friendly identifier of the |
340 |
+ |
* component this class is running in. |
341 |
+ |
* eg, a Filter may be called "filter1", |
342 |
+ |
* If this class does not have an owning |
343 |
+ |
* component, a name from the configuration |
344 |
+ |
* can be placed here. This name could also |
345 |
+ |
* be changed to null for utility classes. |
346 |
+ |
*/ |
347 |
+ |
private String _name = Core.NAME; |
348 |
|
|
349 |
|
//---STATIC ATTRIBUTES--- |
350 |
|
|