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