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