1 | |
package org.apache.ojb.broker.util.logging; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.ojb.broker.util.configuration.Configuration; |
19 | |
import org.apache.ojb.broker.util.configuration.ConfigurationException; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class PoorMansLoggerImpl implements Logger |
29 | |
{ |
30 | |
protected static final String STR_DEBUG = "DEBUG"; |
31 | |
protected static final String STR_INFO = "INFO"; |
32 | |
protected static final String STR_WARN = "WARN"; |
33 | |
protected static final String STR_ERROR = "ERROR"; |
34 | |
protected static final String STR_FATAL = "FATAL"; |
35 | |
|
36 | |
protected static final String STR_DEBUG_MSG = "DEBUG: "; |
37 | |
protected static final String STR_INFO_MSG = "INFO: "; |
38 | |
protected static final String STR_WARN_MSG = "WARN: "; |
39 | |
protected static final String STR_ERROR_MSG = "ERROR: "; |
40 | |
protected static final String STR_FATAL_MSG = "FATAL: "; |
41 | |
|
42 | |
protected static final String BRAKE_OPEN = "["; |
43 | |
protected static final String BRAKE_CLOSE = "] "; |
44 | |
|
45 | |
private String name ; |
46 | |
|
47 | |
private int level = 0; |
48 | |
|
49 | |
public PoorMansLoggerImpl( String name) |
50 | |
{ |
51 | |
this.name = name ; |
52 | |
} |
53 | |
|
54 | |
protected int getLevel() |
55 | |
{ |
56 | |
return level; |
57 | |
} |
58 | |
|
59 | |
void setLevel(int pLevel) |
60 | |
{ |
61 | |
level = pLevel; |
62 | |
} |
63 | |
|
64 | |
public String getName() |
65 | |
{ |
66 | |
return name; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public void debug(Object pObject) |
74 | |
{ |
75 | |
debug(pObject, null); |
76 | |
} |
77 | |
|
78 | |
public void debug(Object message, Throwable t) |
79 | |
{ |
80 | |
if (DEBUG >= getLevel()) |
81 | |
{ |
82 | |
log(STR_DEBUG_MSG, message, t); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
public void safeDebug(String message,Object obj) |
87 | |
{ |
88 | |
safeDebug(message,obj,null); |
89 | |
} |
90 | |
|
91 | |
public void safeDebug(String message,Object obj,Throwable t) |
92 | |
{ |
93 | |
if(DEBUG >= getLevel()) |
94 | |
{ |
95 | |
String toString = null; |
96 | |
if(obj != null) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | |
toString = obj.toString(); |
101 | |
} |
102 | |
catch(Throwable throwable) |
103 | |
{ |
104 | |
toString = "BAD toString() impl for "+obj.getClass().getName(); |
105 | |
} |
106 | |
} |
107 | |
log(STR_DEBUG_MSG,message + " : " + toString,t); |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void info(Object pObject) |
117 | |
{ |
118 | |
info(pObject, null); |
119 | |
} |
120 | |
|
121 | |
public void info(Object message, Throwable t) |
122 | |
{ |
123 | |
if (INFO >= getLevel()) |
124 | |
{ |
125 | |
log(STR_INFO_MSG, message, t); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
public void safeInfo(String message,Object obj) |
130 | |
{ |
131 | |
safeInfo(message,obj,null); |
132 | |
} |
133 | |
|
134 | |
public void safeInfo(String message,Object obj,Throwable t) |
135 | |
{ |
136 | |
if(INFO >= getLevel()) |
137 | |
{ |
138 | |
String toString = null; |
139 | |
if(obj != null) |
140 | |
{ |
141 | |
try |
142 | |
{ |
143 | |
toString = obj.toString(); |
144 | |
} |
145 | |
catch(Throwable throwable) |
146 | |
{ |
147 | |
toString = "BAD toString() impl for "+obj.getClass().getName(); |
148 | |
} |
149 | |
} |
150 | |
log(STR_INFO_MSG, message + " : " + toString,t); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void warn(Object pObject) |
159 | |
{ |
160 | |
warn(pObject, null); |
161 | |
} |
162 | |
|
163 | |
public void warn(Object message, Throwable t) |
164 | |
{ |
165 | |
if (WARN >= getLevel()) |
166 | |
{ |
167 | |
log(STR_WARN_MSG, message, t); |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
public void safeWarn(String message,Object obj) |
172 | |
{ |
173 | |
safeWarn(message,obj,null); |
174 | |
} |
175 | |
|
176 | |
public void safeWarn(String message,Object obj,Throwable t) |
177 | |
{ |
178 | |
if(WARN >= getLevel()) |
179 | |
{ |
180 | |
String toString = null; |
181 | |
if(obj != null) |
182 | |
{ |
183 | |
try |
184 | |
{ |
185 | |
toString = obj.toString(); |
186 | |
} |
187 | |
catch(Throwable throwable) |
188 | |
{ |
189 | |
toString = "BAD toString() impl for "+obj.getClass().getName(); |
190 | |
} |
191 | |
} |
192 | |
log(STR_WARN_MSG,message + " : " + toString,t); |
193 | |
} |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public void error(Object pObject) |
201 | |
{ |
202 | |
error(pObject, null); |
203 | |
} |
204 | |
|
205 | |
public void error(Object message, Throwable t) |
206 | |
{ |
207 | |
if (ERROR >= getLevel()) |
208 | |
{ |
209 | |
log(STR_ERROR_MSG, message, t); |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
public void safeError(String message,Object obj) |
214 | |
{ |
215 | |
safeError(message,obj,null); |
216 | |
} |
217 | |
|
218 | |
public void safeError(String message,Object obj,Throwable t) |
219 | |
{ |
220 | |
if(ERROR >= getLevel()) |
221 | |
{ |
222 | |
String toString = null; |
223 | |
if(obj != null) |
224 | |
{ |
225 | |
try |
226 | |
{ |
227 | |
toString = obj.toString(); |
228 | |
} |
229 | |
catch(Throwable throwable) |
230 | |
{ |
231 | |
toString = "BAD toString() impl for "+obj.getClass().getName(); |
232 | |
} |
233 | |
} |
234 | |
log(STR_ERROR_MSG,message + " : " + toString,t); |
235 | |
} |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public void fatal(Object pObject) |
243 | |
{ |
244 | |
fatal(pObject, null); |
245 | |
} |
246 | |
|
247 | |
public void fatal(Object message, Throwable t) |
248 | |
{ |
249 | |
if (FATAL >= getLevel()) |
250 | |
{ |
251 | |
log(STR_FATAL_MSG, message, t); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
public void safeFatal(String message,Object obj) |
256 | |
{ |
257 | |
safeFatal(message,obj,null); |
258 | |
} |
259 | |
|
260 | |
public void safeFatal(String message,Object obj,Throwable t) |
261 | |
{ |
262 | |
if(FATAL >= getLevel()) |
263 | |
{ |
264 | |
String toString = null; |
265 | |
if(obj != null) |
266 | |
{ |
267 | |
try |
268 | |
{ |
269 | |
toString = obj.toString(); |
270 | |
} |
271 | |
catch(Throwable throwable) |
272 | |
{ |
273 | |
toString = "BAD toString() impl for "+obj.getClass().getName(); |
274 | |
} |
275 | |
} |
276 | |
log(STR_FATAL_MSG,message + " : " + toString,t); |
277 | |
} |
278 | |
} |
279 | |
|
280 | |
|
281 | |
public boolean isDebugEnabled() |
282 | |
{ |
283 | |
return isEnabledFor(DEBUG); |
284 | |
} |
285 | |
|
286 | |
public boolean isEnabledFor(int priority) |
287 | |
{ |
288 | |
return priority >= getLevel(); |
289 | |
} |
290 | |
|
291 | |
protected void log(String aLevel, Object obj, Throwable t) |
292 | |
{ |
293 | |
System.out.print(BRAKE_OPEN + name + BRAKE_CLOSE + aLevel); |
294 | |
if (obj != null && obj instanceof Throwable) |
295 | |
{ |
296 | |
try |
297 | |
{ |
298 | |
System.out.println(((Throwable) obj).getMessage()); |
299 | |
((Throwable) obj).printStackTrace(); |
300 | |
} |
301 | |
catch (Throwable ignored) |
302 | |
{ |
303 | |
|
304 | |
} |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | |
System.out.println(obj); |
309 | |
} |
310 | |
|
311 | |
if (t != null) |
312 | |
{ |
313 | |
try |
314 | |
{ |
315 | |
System.out.println(t.getMessage()); |
316 | |
t.printStackTrace(); |
317 | |
} |
318 | |
catch (Throwable ignored) |
319 | |
{ |
320 | |
|
321 | |
} |
322 | |
} |
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public void configure(Configuration config) throws ConfigurationException |
329 | |
{ |
330 | |
LoggingConfiguration lc = (LoggingConfiguration) config; |
331 | |
String levelName = lc.getLogLevel(name); |
332 | |
setLevel(levelName); |
333 | |
} |
334 | |
|
335 | |
public void setLevel(String levelName) |
336 | |
{ |
337 | |
if (levelName.equalsIgnoreCase(STR_DEBUG)) |
338 | |
{ |
339 | |
level = DEBUG; |
340 | |
} |
341 | |
else if (levelName.equalsIgnoreCase(STR_INFO)) |
342 | |
{ |
343 | |
level = INFO; |
344 | |
} |
345 | |
else if (levelName.equalsIgnoreCase(STR_WARN)) |
346 | |
{ |
347 | |
level = WARN; |
348 | |
} |
349 | |
else if (levelName.equalsIgnoreCase(STR_ERROR)) |
350 | |
{ |
351 | |
level = ERROR; |
352 | |
} |
353 | |
else if (levelName.equalsIgnoreCase(STR_FATAL)) |
354 | |
{ |
355 | |
level = FATAL; |
356 | |
} |
357 | |
else |
358 | |
{ |
359 | |
level = WARN; |
360 | |
} |
361 | |
} |
362 | |
} |