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