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