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