1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.impl;
19
20 import java.io.InputStream;
21 import java.io.Serializable;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Properties;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogConfigurationException;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class SimpleLog implements Log, Serializable {
76
77
78
79
80
81 static protected final String systemPrefix =
82 "org.apache.commons.logging.simplelog.";
83
84
85 static protected final Properties simpleLogProps = new Properties();
86
87
88 static protected final String DEFAULT_DATE_TIME_FORMAT =
89 "yyyy/MM/dd HH:mm:ss:SSS zzz";
90
91
92 static protected boolean showLogName = false;
93
94
95
96
97 static protected boolean showShortName = true;
98
99 static protected boolean showDateTime = false;
100
101 static protected String dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
102
103 static protected DateFormat dateFormatter = null;
104
105
106
107
108
109 public static final int LOG_LEVEL_TRACE = 1;
110
111 public static final int LOG_LEVEL_DEBUG = 2;
112
113 public static final int LOG_LEVEL_INFO = 3;
114
115 public static final int LOG_LEVEL_WARN = 4;
116
117 public static final int LOG_LEVEL_ERROR = 5;
118
119 public static final int LOG_LEVEL_FATAL = 6;
120
121
122 public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1);
123
124
125 public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1);
126
127
128
129 private static String getStringProperty(String name) {
130 String prop = null;
131 try {
132 prop = System.getProperty(name);
133 } catch (SecurityException e) {
134 ;
135 }
136 return (prop == null) ? simpleLogProps.getProperty(name) : prop;
137 }
138
139 private static String getStringProperty(String name, String dephault) {
140 String prop = getStringProperty(name);
141 return (prop == null) ? dephault : prop;
142 }
143
144 private static boolean getBooleanProperty(String name, boolean dephault) {
145 String prop = getStringProperty(name);
146 return (prop == null) ? dephault : "true".equalsIgnoreCase(prop);
147 }
148
149
150
151
152 static {
153
154 InputStream in = getResourceAsStream("simplelog.properties");
155 if(null != in) {
156 try {
157 simpleLogProps.load(in);
158 in.close();
159 } catch(java.io.IOException e) {
160
161 }
162 }
163
164 showLogName = getBooleanProperty( systemPrefix + "showlogname", showLogName);
165 showShortName = getBooleanProperty( systemPrefix + "showShortLogname", showShortName);
166 showDateTime = getBooleanProperty( systemPrefix + "showdatetime", showDateTime);
167
168 if(showDateTime) {
169 dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat",
170 dateTimeFormat);
171 try {
172 dateFormatter = new SimpleDateFormat(dateTimeFormat);
173 } catch(IllegalArgumentException e) {
174
175 dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
176 dateFormatter = new SimpleDateFormat(dateTimeFormat);
177 }
178 }
179 }
180
181
182
183
184
185 protected String logName = null;
186
187 protected int currentLogLevel;
188
189 private String shortLogName = null;
190
191
192
193
194
195
196
197
198
199 public SimpleLog(String name) {
200
201 logName = name;
202
203
204
205
206 setLevel(SimpleLog.LOG_LEVEL_INFO);
207
208
209 String lvl = getStringProperty(systemPrefix + "log." + logName);
210 int i = String.valueOf(name).lastIndexOf(".");
211 while(null == lvl && i > -1) {
212 name = name.substring(0,i);
213 lvl = getStringProperty(systemPrefix + "log." + name);
214 i = String.valueOf(name).lastIndexOf(".");
215 }
216
217 if(null == lvl) {
218 lvl = getStringProperty(systemPrefix + "defaultlog");
219 }
220
221 if("all".equalsIgnoreCase(lvl)) {
222 setLevel(SimpleLog.LOG_LEVEL_ALL);
223 } else if("trace".equalsIgnoreCase(lvl)) {
224 setLevel(SimpleLog.LOG_LEVEL_TRACE);
225 } else if("debug".equalsIgnoreCase(lvl)) {
226 setLevel(SimpleLog.LOG_LEVEL_DEBUG);
227 } else if("info".equalsIgnoreCase(lvl)) {
228 setLevel(SimpleLog.LOG_LEVEL_INFO);
229 } else if("warn".equalsIgnoreCase(lvl)) {
230 setLevel(SimpleLog.LOG_LEVEL_WARN);
231 } else if("error".equalsIgnoreCase(lvl)) {
232 setLevel(SimpleLog.LOG_LEVEL_ERROR);
233 } else if("fatal".equalsIgnoreCase(lvl)) {
234 setLevel(SimpleLog.LOG_LEVEL_FATAL);
235 } else if("off".equalsIgnoreCase(lvl)) {
236 setLevel(SimpleLog.LOG_LEVEL_OFF);
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249 public void setLevel(int currentLogLevel) {
250
251 this.currentLogLevel = currentLogLevel;
252
253 }
254
255
256
257
258
259 public int getLevel() {
260
261 return currentLogLevel;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 protected void log(int type, Object message, Throwable t) {
278
279 StringBuffer buf = new StringBuffer();
280
281
282 if(showDateTime) {
283 buf.append(dateFormatter.format(new Date()));
284 buf.append(" ");
285 }
286
287
288 switch(type) {
289 case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break;
290 case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break;
291 case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO] "); break;
292 case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN] "); break;
293 case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break;
294 case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break;
295 }
296
297
298 if( showShortName) {
299 if( shortLogName==null ) {
300
301 shortLogName = logName.substring(logName.lastIndexOf(".") + 1);
302 shortLogName =
303 shortLogName.substring(shortLogName.lastIndexOf("/") + 1);
304 }
305 buf.append(String.valueOf(shortLogName)).append(" - ");
306 } else if(showLogName) {
307 buf.append(String.valueOf(logName)).append(" - ");
308 }
309
310
311 buf.append(String.valueOf(message));
312
313
314 if(t != null) {
315 buf.append(" <");
316 buf.append(t.toString());
317 buf.append(">");
318
319 java.io.StringWriter sw= new java.io.StringWriter(1024);
320 java.io.PrintWriter pw= new java.io.PrintWriter(sw);
321 t.printStackTrace(pw);
322 pw.close();
323 buf.append(sw.toString());
324 }
325
326
327 write(buf);
328
329 }
330
331
332
333
334
335
336
337
338
339
340 protected void write(StringBuffer buffer) {
341
342 System.err.println(buffer.toString());
343
344 }
345
346
347
348
349
350
351
352 protected boolean isLevelEnabled(int logLevel) {
353
354
355 return (logLevel >= currentLogLevel);
356 }
357
358
359
360
361
362
363
364
365 public final void debug(Object message) {
366
367 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
368 log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
369 }
370 }
371
372
373
374
375
376 public final void debug(Object message, Throwable t) {
377
378 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
379 log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
380 }
381 }
382
383
384
385
386
387 public final void trace(Object message) {
388
389 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
390 log(SimpleLog.LOG_LEVEL_TRACE, message, null);
391 }
392 }
393
394
395
396
397
398 public final void trace(Object message, Throwable t) {
399
400 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
401 log(SimpleLog.LOG_LEVEL_TRACE, message, t);
402 }
403 }
404
405
406
407
408
409 public final void info(Object message) {
410
411 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
412 log(SimpleLog.LOG_LEVEL_INFO,message,null);
413 }
414 }
415
416
417
418
419
420 public final void info(Object message, Throwable t) {
421
422 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
423 log(SimpleLog.LOG_LEVEL_INFO, message, t);
424 }
425 }
426
427
428
429
430
431 public final void warn(Object message) {
432
433 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
434 log(SimpleLog.LOG_LEVEL_WARN, message, null);
435 }
436 }
437
438
439
440
441
442 public final void warn(Object message, Throwable t) {
443
444 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
445 log(SimpleLog.LOG_LEVEL_WARN, message, t);
446 }
447 }
448
449
450
451
452
453 public final void error(Object message) {
454
455 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
456 log(SimpleLog.LOG_LEVEL_ERROR, message, null);
457 }
458 }
459
460
461
462
463
464 public final void error(Object message, Throwable t) {
465
466 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
467 log(SimpleLog.LOG_LEVEL_ERROR, message, t);
468 }
469 }
470
471
472
473
474
475 public final void fatal(Object message) {
476
477 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
478 log(SimpleLog.LOG_LEVEL_FATAL, message, null);
479 }
480 }
481
482
483
484
485
486 public final void fatal(Object message, Throwable t) {
487
488 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
489 log(SimpleLog.LOG_LEVEL_FATAL, message, t);
490 }
491 }
492
493
494
495
496
497
498
499
500
501 public final boolean isDebugEnabled() {
502
503 return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
504 }
505
506
507
508
509
510
511
512
513
514 public final boolean isErrorEnabled() {
515
516 return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
517 }
518
519
520
521
522
523
524
525
526
527 public final boolean isFatalEnabled() {
528
529 return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
530 }
531
532
533
534
535
536
537
538
539
540 public final boolean isInfoEnabled() {
541
542 return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
543 }
544
545
546
547
548
549
550
551
552
553 public final boolean isTraceEnabled() {
554
555 return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
556 }
557
558
559
560
561
562
563
564
565
566 public final boolean isWarnEnabled() {
567
568 return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
569 }
570
571
572
573
574
575
576
577
578
579
580
581
582 private static ClassLoader getContextClassLoader()
583 {
584 ClassLoader classLoader = null;
585
586 if (classLoader == null) {
587 try {
588
589 Method method = Thread.class.getMethod("getContextClassLoader", null);
590
591
592 try {
593 classLoader = (ClassLoader)method.invoke(Thread.currentThread(), null);
594 } catch (IllegalAccessException e) {
595 ;
596 } catch (InvocationTargetException e) {
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613 if (e.getTargetException() instanceof SecurityException) {
614 ;
615 } else {
616
617
618 throw new LogConfigurationException
619 ("Unexpected InvocationTargetException", e.getTargetException());
620 }
621 }
622 } catch (NoSuchMethodException e) {
623
624 ;
625 }
626 }
627
628 if (classLoader == null) {
629 classLoader = SimpleLog.class.getClassLoader();
630 }
631
632
633 return classLoader;
634 }
635
636 private static InputStream getResourceAsStream(final String name)
637 {
638 return (InputStream)AccessController.doPrivileged(
639 new PrivilegedAction() {
640 public Object run() {
641 ClassLoader threadCL = getContextClassLoader();
642
643 if (threadCL != null) {
644 return threadCL.getResourceAsStream(name);
645 } else {
646 return ClassLoader.getSystemResourceAsStream(name);
647 }
648 }
649 });
650 }
651 }
652