1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import java.util.Date; |
5 |
import java.text.DateFormat; |
6 |
import java.util.Locale; |
7 |
|
8 |
/** |
9 |
* The ScreenLoggerServant is an implementation of the Logger defined |
10 |
* in the IDL interface. It's only purpose is to simply print all the |
11 |
* logging information it receives to the screen. |
12 |
* |
13 |
* @author $Author: tdb1 $ |
14 |
* @version $Id: ScreenLoggerServant.java,v 1.7 2000/11/16 17:59:40 tdb1 Exp $ |
15 |
*/ |
16 |
class Logger { |
17 |
|
18 |
//---FINAL ATTRIBUTES--- |
19 |
|
20 |
/** |
21 |
* The current CVS revision of this class |
22 |
*/ |
23 |
public final String REVISION = "$Revision: 1.7 $"; |
24 |
|
25 |
//---STATIC METHODS--- |
26 |
|
27 |
//---CONSTRUCTORS--- |
28 |
|
29 |
/** |
30 |
* Creates a new ScreenLoggerServant. |
31 |
* |
32 |
* @param verbosityLevel the verbosity level that this instance will log at |
33 |
*/ |
34 |
public Logger(int verbosityLevel) { |
35 |
_verbosityLevel = verbosityLevel; |
36 |
write(this.toString(), Logger.SYSINIT, "started"); |
37 |
} |
38 |
|
39 |
//---PUBLIC METHODS--- |
40 |
|
41 |
/** |
42 |
* The write() method takes a message, formats it using the |
43 |
* formatLogLine() method, and then outputs it to the screen |
44 |
* using System.out.println(). The source is usually the |
45 |
* calling object, referenced by `this'. The method has been |
46 |
* made synchronized to avoid it being called by two different |
47 |
* objects and the output ending up merged on the screen. |
48 |
* |
49 |
* @param source A string representation of the calling object. |
50 |
* @param verbosity the verbosity of this message |
51 |
* @param message The text to be logged. |
52 |
*/ |
53 |
public void write(String source, int verbosity, String message) { |
54 |
if (verbosity <= _verbosityLevel) { |
55 |
String line = formatLogLine(source, message); |
56 |
System.out.println(line); |
57 |
} |
58 |
} |
59 |
|
60 |
/** |
61 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
62 |
* method to provide clean logging (every class should have this). |
63 |
* |
64 |
* @return the name of this class and its CVS revision |
65 |
*/ |
66 |
public String toString() { |
67 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
68 |
} |
69 |
//---PRIVATE METHODS--- |
70 |
|
71 |
/** |
72 |
* This method generates a nicely formatted line for the log, |
73 |
* including the date/time and the source of the message. The date |
74 |
* and time are formatted using the DateFormat class, and the source |
75 |
* class is formatted using the toString() method found in every |
76 |
* source file. This is then prepended to the message and returned. |
77 |
* |
78 |
* @param source A string representation of the calling object. |
79 |
* @param message The message to be logged. |
80 |
* @return The string to be written to the log. |
81 |
*/ |
82 |
private String formatLogLine(String source, String message){ |
83 |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
84 |
return "[" + date + "] " + source + ": " + message; |
85 |
} |
86 |
|
87 |
//---ACCESSOR/MUTATOR METHODS--- |
88 |
|
89 |
//---ATTRIBUTES--- |
90 |
|
91 |
/** |
92 |
* The verbosity level of this instance |
93 |
*/ |
94 |
private int _verbosityLevel; |
95 |
|
96 |
//---STATIC ATTRIBUTES--- |
97 |
|
98 |
public static final int FATAL=0; |
99 |
public static final int ERROR=1; |
100 |
public static final int WARNING=2; |
101 |
public static final int SYSMSG=3; |
102 |
public static final int SYSINIT=4; |
103 |
public static final int DEBUG=5; |
104 |
|
105 |
} |