1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.2 |
package uk.ac.ukc.iscream.core.loggers; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.3 |
import uk.ac.ukc.iscream.util.*; |
6 |
|
|
import uk.ac.ukc.iscream.core.*; |
7 |
tdb |
1.1 |
import java.util.Date; |
8 |
|
|
import java.text.DateFormat; |
9 |
|
|
import java.util.Locale; |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* The ScreenLogger is an implementation of the LoggerImpl defined |
13 |
|
|
* in the associated interface. It's only purpose is to simply print |
14 |
|
|
* all the logging information it receives to the screen. |
15 |
|
|
* |
16 |
ajm |
1.3 |
* @author $Author: ajm4 $ |
17 |
|
|
* @version $Id: ScreenLogger.java,v 1.2 2000/11/29 21:27:25 ajm4 Exp $ |
18 |
tdb |
1.1 |
*/ |
19 |
ajm |
1.2 |
public class ScreenLogger implements LoggerImpl { |
20 |
tdb |
1.1 |
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
27 |
tdb |
1.1 |
|
28 |
|
|
//---STATIC METHODS--- |
29 |
|
|
|
30 |
|
|
//---CONSTRUCTORS--- |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Creates a new ScreenLoggerServant. |
34 |
|
|
*/ |
35 |
|
|
public ScreenLogger() { |
36 |
ajm |
1.3 |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
37 |
|
|
write(toString(), Logger.SYSINIT, "started"); |
38 |
|
|
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
39 |
tdb |
1.1 |
} |
40 |
|
|
|
41 |
|
|
//---PUBLIC METHODS--- |
42 |
|
|
|
43 |
|
|
/** |
44 |
ajm |
1.3 |
* The write() method takes a message, formats it using the |
45 |
|
|
* formatLogLine() method, and then outputs it to the screen |
46 |
|
|
* using System.out.println(). The source is usually the |
47 |
|
|
* calling object, referenced by `this'. The method has been |
48 |
|
|
* made synchronized to avoid it being called by two different |
49 |
|
|
* objects and the output ending up merged on the screen. |
50 |
|
|
* |
51 |
|
|
* @param source A string representation of the calling object. |
52 |
|
|
* @param verbosity the verbosity of this message |
53 |
|
|
* @param message The text to be logged. |
54 |
|
|
*/ |
55 |
tdb |
1.1 |
public synchronized void write(String source, int verbosity, String message) { |
56 |
ajm |
1.3 |
if (verbosity <= _verbosityLevel) { |
57 |
|
|
String line = formatLogLine(source, message); |
58 |
|
|
System.out.println(line); |
59 |
tdb |
1.1 |
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
64 |
|
|
* method to provide clean logging (every class should have this). |
65 |
|
|
* |
66 |
ajm |
1.3 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
67 |
|
|
* to format the toString() |
68 |
|
|
* |
69 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
70 |
|
|
*/ |
71 |
|
|
public String toString() { |
72 |
ajm |
1.3 |
return FormatName.getName( |
73 |
|
|
_name, |
74 |
|
|
getClass().getName(), |
75 |
|
|
REVISION); |
76 |
tdb |
1.1 |
} |
77 |
ajm |
1.3 |
|
78 |
tdb |
1.1 |
//---PRIVATE METHODS--- |
79 |
|
|
|
80 |
ajm |
1.3 |
/** |
81 |
|
|
* This method generates a nicely formatted line for the log, |
82 |
|
|
* including the date/time and the source of the message. The date |
83 |
|
|
* and time are formatted using the DateFormat class, and the source |
84 |
|
|
* class is formatted using the toString() method found in every |
85 |
|
|
* source file. This is then prepended to the message and returned. |
86 |
|
|
* |
87 |
|
|
* @param source A string representation of the calling object. |
88 |
|
|
* @param message The message to be logged. |
89 |
|
|
* @return The string to be written to the log. |
90 |
|
|
*/ |
91 |
tdb |
1.1 |
private String formatLogLine(String source, String message){ |
92 |
|
|
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
93 |
|
|
return "[" + date + "] " + source + ": " + message; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
97 |
|
|
|
98 |
|
|
//---ATTRIBUTES--- |
99 |
|
|
|
100 |
ajm |
1.3 |
/** |
101 |
|
|
* The verbosity level of this instance |
102 |
|
|
*/ |
103 |
|
|
private int _verbosityLevel; |
104 |
|
|
|
105 |
|
|
/** |
106 |
|
|
* This is the friendly identifier of the |
107 |
|
|
* component this class is running in. |
108 |
|
|
* eg, a Filter may be called "filter1", |
109 |
|
|
* If this class does not have an owning |
110 |
|
|
* component, a name from the configuration |
111 |
|
|
* can be placed here. This name could also |
112 |
|
|
* be changed to null for utility classes. |
113 |
|
|
*/ |
114 |
|
|
private String _name = Core.NAME; |
115 |
tdb |
1.1 |
|
116 |
|
|
//---STATIC ATTRIBUTES--- |
117 |
|
|
|
118 |
|
|
} |