1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.log;
18
19 import java.io.PrintStream;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 import org.apache.commons.logging.Log;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @Deprecated
44 public class SimpleLog implements Log {
45
46
47
48 public static final int LOG_LEVEL_TRACE = 1;
49
50
51 public static final int LOG_LEVEL_DEBUG = 2;
52
53
54 public static final int LOG_LEVEL_INFO = 3;
55
56
57 public static final int LOG_LEVEL_WARN = 4;
58
59
60 public static final int LOG_LEVEL_ERROR = 5;
61
62
63 public static final int LOG_LEVEL_FATAL = 6;
64
65
66 public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1);
67
68
69 public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1);
70
71
72
73 static protected final String PROP_LEVEL =
74 "org.apache.commons.discovery.log.level";
75
76
77 static protected boolean showLogName = false;
78
79
80
81
82
83 static protected boolean showShortName = true;
84
85
86 static protected boolean showDateTime = false;
87
88
89 static protected DateFormat dateFormatter = null;
90
91
92 static protected int logLevel = LOG_LEVEL_INFO;
93
94
95
96
97
98 static private PrintStream out = System.out;
99
100
101
102
103 static {
104 if(showDateTime) {
105 dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS zzz");
106 }
107
108 try {
109
110 String lvl = System.getProperty(PROP_LEVEL);
111
112 if("all".equalsIgnoreCase(lvl)) {
113 setLevel(SimpleLog.LOG_LEVEL_ALL);
114 } else if("trace".equalsIgnoreCase(lvl)) {
115 setLevel(SimpleLog.LOG_LEVEL_TRACE);
116 } else if("debug".equalsIgnoreCase(lvl)) {
117 setLevel(SimpleLog.LOG_LEVEL_DEBUG);
118 } else if("info".equalsIgnoreCase(lvl)) {
119 setLevel(SimpleLog.LOG_LEVEL_INFO);
120 } else if("warn".equalsIgnoreCase(lvl)) {
121 setLevel(SimpleLog.LOG_LEVEL_WARN);
122 } else if("error".equalsIgnoreCase(lvl)) {
123 setLevel(SimpleLog.LOG_LEVEL_ERROR);
124 } else if("fatal".equalsIgnoreCase(lvl)) {
125 setLevel(SimpleLog.LOG_LEVEL_FATAL);
126 } else if("off".equalsIgnoreCase(lvl)) {
127 setLevel(SimpleLog.LOG_LEVEL_OFF);
128 }
129 } catch (SecurityException ignored) {
130
131
132
133
134 }
135
136 }
137
138
139
140
141
142
143
144
145 public static void setLevel(int currentLogLevel) {
146 logLevel = currentLogLevel;
147 }
148
149
150
151
152
153
154 public static int getLevel() {
155 return logLevel;
156 }
157
158
159
160
161
162
163
164 protected static boolean isLevelEnabled(int level) {
165
166
167 return (level >= getLevel());
168 }
169
170
171
172
173 protected String logName = null;
174
175 private String prefix=null;
176
177
178
179
180
181
182
183
184 public SimpleLog(String name) {
185 logName = name;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199 protected void log(int type, Object message, Throwable t) {
200
201 StringBuffer buf = new StringBuffer();
202
203
204 if(showDateTime) {
205 buf.append(dateFormatter.format(new Date()));
206 buf.append(" ");
207 }
208
209
210 switch(type) {
211 case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break;
212 case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break;
213 case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO ] "); break;
214 case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN ] "); break;
215 case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break;
216 case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break;
217 }
218
219
220 if( showShortName) {
221 if( prefix==null ) {
222
223 prefix = logName.substring( logName.lastIndexOf(".") +1) + " - ";
224 prefix = prefix.substring( prefix.lastIndexOf("/") +1) + "-";
225 }
226 buf.append( prefix );
227 } else if(showLogName) {
228 buf.append(String.valueOf(logName)).append(" - ");
229 }
230
231
232 buf.append(String.valueOf(message));
233
234
235 if(t != null) {
236 buf.append(" <");
237 buf.append(t.toString());
238 buf.append(">");
239 }
240
241
242 out.println(buf.toString());
243
244 if (t != null) {
245 t.printStackTrace(System.err);
246 }
247 }
248
249
250
251
252
253
254
255
256 public final void debug(Object message) {
257 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
258 log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
259 }
260 }
261
262
263
264
265
266
267
268 public final void debug(Object message, Throwable t) {
269 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
270 log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
271 }
272 }
273
274
275
276
277
278
279 public final void trace(Object message) {
280 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
281 log(SimpleLog.LOG_LEVEL_TRACE, message, null);
282 }
283 }
284
285
286
287
288
289
290
291 public final void trace(Object message, Throwable t) {
292 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
293 log(SimpleLog.LOG_LEVEL_TRACE, message, t);
294 }
295 }
296
297
298
299
300
301
302 public final void info(Object message) {
303 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
304 log(SimpleLog.LOG_LEVEL_INFO,message,null);
305 }
306 }
307
308
309
310
311
312
313
314 public final void info(Object message, Throwable t) {
315 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
316 log(SimpleLog.LOG_LEVEL_INFO, message, t);
317 }
318 }
319
320
321
322
323
324
325 public final void warn(Object message) {
326 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
327 log(SimpleLog.LOG_LEVEL_WARN, message, null);
328 }
329 }
330
331
332
333
334
335
336
337 public final void warn(Object message, Throwable t) {
338 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
339 log(SimpleLog.LOG_LEVEL_WARN, message, t);
340 }
341 }
342
343
344
345
346
347
348 public final void error(Object message) {
349 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
350 log(SimpleLog.LOG_LEVEL_ERROR, message, null);
351 }
352 }
353
354
355
356
357
358
359
360 public final void error(Object message, Throwable t) {
361 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
362 log(SimpleLog.LOG_LEVEL_ERROR, message, t);
363 }
364 }
365
366
367
368
369
370
371 public final void fatal(Object message) {
372 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
373 log(SimpleLog.LOG_LEVEL_FATAL, message, null);
374 }
375 }
376
377
378
379
380
381
382
383 public final void fatal(Object message, Throwable t) {
384 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
385 log(SimpleLog.LOG_LEVEL_FATAL, message, t);
386 }
387 }
388
389
390
391
392
393
394
395
396
397
398 public final boolean isDebugEnabled() {
399 return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
400 }
401
402
403
404
405
406
407
408
409
410
411 public final boolean isErrorEnabled() {
412 return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
413 }
414
415
416
417
418
419
420
421
422
423
424 public final boolean isFatalEnabled() {
425 return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
426 }
427
428
429
430
431
432
433
434
435
436
437 public final boolean isInfoEnabled() {
438 return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
439 }
440
441
442
443
444
445
446
447
448
449
450 public final boolean isTraceEnabled() {
451 return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
452 }
453
454
455
456
457
458
459
460
461
462
463 public final boolean isWarnEnabled() {
464 return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
465 }
466
467 }