1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.core.loggers; |
3 |
|
4 |
//---IMPORTS--- |
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; |
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 |
* @author $Author: ajm4 $ |
17 |
* @version $Id: ScreenLogger.java,v 1.2 2000/11/29 21:27:25 ajm4 Exp $ |
18 |
*/ |
19 |
public class ScreenLogger implements LoggerImpl { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.2 $"; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
/** |
33 |
* Creates a new ScreenLoggerServant. |
34 |
*/ |
35 |
public ScreenLogger() { |
36 |
_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 |
} |
40 |
|
41 |
//---PUBLIC METHODS--- |
42 |
|
43 |
/** |
44 |
* 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 |
public synchronized void write(String source, int verbosity, String message) { |
56 |
if (verbosity <= _verbosityLevel) { |
57 |
String line = formatLogLine(source, message); |
58 |
System.out.println(line); |
59 |
} |
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 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
67 |
* to format the toString() |
68 |
* |
69 |
* @return the name of this class and its CVS revision |
70 |
*/ |
71 |
public String toString() { |
72 |
return FormatName.getName( |
73 |
_name, |
74 |
getClass().getName(), |
75 |
REVISION); |
76 |
} |
77 |
|
78 |
//---PRIVATE METHODS--- |
79 |
|
80 |
/** |
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 |
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 |
/** |
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 |
|
116 |
//---STATIC ATTRIBUTES--- |
117 |
|
118 |
} |