1 package attrib4j;
2
3 /***
4 * Simple helper class that allows to send debug messages to the Java
5 * console according to the tracing level. Log levels are from standard
6 * unix syslog.
7 * TODO Switch to levels used in log4j or jdk1.4
8 * @author <a href="mailto:mpollack@speakeasy.net">Mark Pollack</a>
9 * @version $Id: Log.java,v 1.9 2003/09/01 22:02:42 markpollack Exp $
10 */
11
12 public class Log {
13
14 /***
15 * Message that contain information normally of use only when
16 * debugging a program.
17 */
18 public static int DEBUG = 0;
19 /***
20 * Informational messages.
21 */
22 public static int INFO = 1;
23
24 /***
25 * Conditions that are not error conditions, but that may require special
26 * handling.
27 */
28 public static int NOTICE = 2;
29 /***
30 * Warning messages
31 */
32 public static int WARNING = 3;
33 /***
34 * Errors.
35 */
36 public static int ERROR = 4;
37 /***
38 * Critical conditions.
39 */
40 public static int CRITICAL = 5;
41 /***
42 * A condition that should be corrected immediately.
43 */
44 public static int ALERT = 6;
45 /***
46 * A panic condition.
47 */
48 public static int EMERGENCY = 7;
49
50 /***
51 * Debug level
52 *
53 * Note: It has been made public for performance
54 * optimization to reduce overhead of including tracing messages
55 */
56 public static int LEVEL = DEBUG;
57
58 private static String[] LEVELMAP =
59 {
60 "DEBUG",
61 "INFO",
62 "NOTICE",
63 "WARNING",
64 "ERROR",
65 "CRITICAL",
66 "ALERT",
67 "EMERGENCY" };
68
69 public Log() {
70
71 }
72
73 /***
74 * Simply outputs a message on the console.
75 */
76 static void out(String finalMessage) {
77 // System.out.println("\n" + new Date(System.currentTimeMillis()));
78 System.out.println(finalMessage);
79 }
80
81 /***
82 * Quick log method signature.
83 * Only if the global level is set below zero will these messages not
84 * be printed.
85 *
86 * @param message a value of type 'String'
87 */
88 public static void log(String message) {
89 // if (LEVEL >= Log.ERROR) {
90 // out(message);
91 // }
92 log(message, INFO);
93 }
94
95 /***
96 * A log message with a given level.
97 */
98 public static void log(String message, int level) {
99 if (level >= LEVEL) {
100 log(message, level, "Attrib4j");
101 }
102 }
103
104 /***
105 * A log message with a level and the class that called this method.
106 *
107 * @param message Log Message
108 * @param level Log level
109 * @param object Callers class.
110 */
111 public static void log(String message, int level, Object object) {
112 if (level >= LEVEL) {
113 SB.delete(0, SB.length());
114 if (object instanceof String) {
115 SB.append(object.toString());
116 } else {
117 SB.append(object.getClass().getName());
118 }
119 SB.append(":");
120 SB.append(LEVELMAP[level]);
121 SB.append(":");
122 SB.append(message);
123 out(SB.toString());
124 }
125 }
126
127 /***
128 * Convenience method to pick the debug level using the method name.
129 *
130 * @param o The callers class.
131 * @param message Log mesasge.
132 */
133 public static void debug(Object o, String message) {
134 log(message, DEBUG, o);
135 }
136
137 /***
138 *
139 * Convenience method to pick the error level using the method nam.e
140 *
141 * @param o The callers class
142 * @param message Log Message
143 */
144 public static void error(Object o, String message) {
145 log(message, ERROR, o);
146 }
147
148 /***
149 * Convenience method to pick the debug level using method name.
150 *
151 * @param message a value of type 'String'
152 */
153 public static void info(String message) {
154 log(message, INFO);
155 }
156
157 /***
158 * Convenience method to pick the debug level using the method name.
159 *
160 * @param o The callers class.
161 * @param message Log mesasge.
162 */
163 public static void info(Object o, String message) {
164 log(message, INFO, o);
165 }
166
167 /***
168 * Convenience method to pick the debug level using method name.
169 *
170 * @param message a value of type 'String'
171 */
172 public static void debug(String message) {
173 log(message, DEBUG);
174 }
175
176 /***
177 * Set the log level for future invocations.
178 *
179 * @param level a value of type 'int'
180 */
181 public static void setLevel(int level) {
182 LEVEL = level;
183 }
184
185 /***
186 * Return the log level.
187 */
188 public static int getLevel() {
189 return LEVEL;
190 }
191
192 private static StringBuffer SB = new StringBuffer();
193
194 } // Log
This page was automatically generated by Maven