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", showShortName);
168 showDateTime = getBooleanProperty(systemPrefix + "showdatetime", showDateTime);
169
170 if (showDateTime) {
171 dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat", dateTimeFormat);
172 try {
173 dateFormatter = new SimpleDateFormat(dateTimeFormat);
174 } catch (IllegalArgumentException e) {
175
176 dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
177 dateFormatter = new SimpleDateFormat(dateTimeFormat);
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
250
251 public void setLevel(int currentLogLevel) {
252
253 this.currentLogLevel = currentLogLevel;
254
255 }
256
257
258
259
260
261
262 public int getLevel() {
263
264 return currentLogLevel;
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 protected void log(int type, Object message, Throwable t) {
283
284 StringBuffer buf = new StringBuffer();
285
286
287 if (showDateTime) {
288 buf.append(dateFormatter.format(new Date()));
289 buf.append(" ");
290 }
291
292
293 switch (type) {
294 case SimpleLog.LOG_LEVEL_TRACE:
295 buf.append("[TRACE] ");
296 break;
297 case SimpleLog.LOG_LEVEL_DEBUG:
298 buf.append("[DEBUG] ");
299 break;
300 case SimpleLog.LOG_LEVEL_INFO:
301 buf.append("[INFO] ");
302 break;
303 case SimpleLog.LOG_LEVEL_WARN:
304 buf.append("[WARN] ");
305 break;
306 case SimpleLog.LOG_LEVEL_ERROR:
307 buf.append("[ERROR] ");
308 break;
309 case SimpleLog.LOG_LEVEL_FATAL:
310 buf.append("[FATAL] ");
311 break;
312 }
313
314
315 if (showShortName) {
316 if (shortLogName == null) {
317
318 shortLogName = logName.substring(logName.lastIndexOf(".") + 1);
319 shortLogName = shortLogName.substring(shortLogName.lastIndexOf("/") + 1);
320 }
321 buf.append(String.valueOf(shortLogName)).append(" - ");
322 } else if (showLogName) {
323 buf.append(String.valueOf(logName)).append(" - ");
324 }
325
326
327 buf.append(String.valueOf(message));
328
329
330 if (t != null) {
331 buf.append(" <");
332 buf.append(t.toString());
333 buf.append(">");
334
335 java.io.StringWriter sw = new java.io.StringWriter(1024);
336 java.io.PrintWriter pw = new java.io.PrintWriter(sw);
337 t.printStackTrace(pw);
338 pw.close();
339 buf.append(sw.toString());
340 }
341
342
343 write(buf);
344
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358 protected void write(StringBuffer buffer) {
359
360 System.err.println(buffer.toString());
361
362 }
363
364
365
366
367
368
369
370 protected boolean isLevelEnabled(int logLevel) {
371
372
373 return (logLevel >= currentLogLevel);
374 }
375
376
377
378
379
380
381
382
383 public final void debug(Object message) {
384
385 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
386 log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
387 }
388 }
389
390
391
392
393
394
395 public final void debug(Object message, Throwable t) {
396
397 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
398 log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
399 }
400 }
401
402
403
404
405
406
407 public final void trace(Object message) {
408
409 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
410 log(SimpleLog.LOG_LEVEL_TRACE, message, null);
411 }
412 }
413
414
415
416
417
418
419 public final void trace(Object message, Throwable t) {
420
421 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
422 log(SimpleLog.LOG_LEVEL_TRACE, message, t);
423 }
424 }
425
426
427
428
429
430
431 public final void info(Object message) {
432
433 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
434 log(SimpleLog.LOG_LEVEL_INFO, message, null);
435 }
436 }
437
438
439
440
441
442
443 public final void info(Object message, Throwable t) {
444
445 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
446 log(SimpleLog.LOG_LEVEL_INFO, message, t);
447 }
448 }
449
450
451
452
453
454
455 public final void warn(Object message) {
456
457 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
458 log(SimpleLog.LOG_LEVEL_WARN, message, null);
459 }
460 }
461
462
463
464
465
466
467 public final void warn(Object message, Throwable t) {
468
469 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
470 log(SimpleLog.LOG_LEVEL_WARN, message, t);
471 }
472 }
473
474
475
476
477
478
479 public final void error(Object message) {
480
481 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
482 log(SimpleLog.LOG_LEVEL_ERROR, message, null);
483 }
484 }
485
486
487
488
489
490
491 public final void error(Object message, Throwable t) {
492
493 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
494 log(SimpleLog.LOG_LEVEL_ERROR, message, t);
495 }
496 }
497
498
499
500
501
502
503 public final void fatal(Object message) {
504
505 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
506 log(SimpleLog.LOG_LEVEL_FATAL, message, null);
507 }
508 }
509
510
511
512
513
514
515 public final void fatal(Object message, Throwable t) {
516
517 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
518 log(SimpleLog.LOG_LEVEL_FATAL, message, t);
519 }
520 }
521
522
523
524
525
526
527
528
529
530
531
532 public final boolean isDebugEnabled() {
533
534 return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
535 }
536
537
538
539
540
541
542
543
544
545
546
547 public final boolean isErrorEnabled() {
548
549 return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
550 }
551
552
553
554
555
556
557
558
559
560
561
562 public final boolean isFatalEnabled() {
563
564 return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
565 }
566
567
568
569
570
571
572
573
574
575
576
577 public final boolean isInfoEnabled() {
578
579 return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
580 }
581
582
583
584
585
586
587
588
589
590
591
592 public final boolean isTraceEnabled() {
593
594 return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
595 }
596
597
598
599
600
601
602
603
604
605
606
607 public final boolean isWarnEnabled() {
608
609 return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
610 }
611
612
613
614
615
616
617
618
619
620
621 private static ClassLoader getContextClassLoader() {
622 ClassLoader classLoader = null;
623
624 if (classLoader == null) {
625 try {
626
627 Method method = Thread.class.getMethod("getContextClassLoader");
628
629
630 try {
631 classLoader = (ClassLoader) method.invoke(Thread.currentThread());
632 } catch (IllegalAccessException e) {
633 ;
634 } catch (InvocationTargetException e) {
635
636
637
638
639
640
641
642
643
644
645
646
647
648 if (e.getTargetException() instanceof SecurityException) {
649 ;
650 } else {
651
652
653 throw new LogConfigurationException("Unexpected InvocationTargetException", e.getTargetException());
654 }
655 }
656 } catch (NoSuchMethodException e) {
657
658 ;
659 }
660 }
661
662 if (classLoader == null) {
663 classLoader = SimpleLog.class.getClassLoader();
664 }
665
666
667 return classLoader;
668 }
669
670 private static InputStream getResourceAsStream(final String name) {
671 return AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
672 public InputStream run() {
673 ClassLoader threadCL = getContextClassLoader();
674
675 if (threadCL != null) {
676 return threadCL.getResourceAsStream(name);
677 } else {
678 return ClassLoader.getSystemResourceAsStream(name);
679 }
680 }
681 });
682 }
683 }