1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.impl;
26
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.InputStream;
30 import java.io.PrintStream;
31 import java.security.AccessController;
32 import java.security.PrivilegedAction;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.Properties;
37
38 import org.slf4j.Logger;
39 import org.slf4j.event.LoggingEvent;
40 import org.slf4j.helpers.FormattingTuple;
41 import org.slf4j.helpers.MarkerIgnoringBase;
42 import org.slf4j.helpers.MessageFormatter;
43 import org.slf4j.helpers.Util;
44 import org.slf4j.spi.LocationAwareLogger;
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public class SimpleLogger extends MarkerIgnoringBase {
120
121 private static final long serialVersionUID = -632788891211436180L;
122 private static final String CONFIGURATION_FILE = "simplelogger.properties";
123
124 private static long START_TIME = System.currentTimeMillis();
125 private static final Properties SIMPLE_LOGGER_PROPS = new Properties();
126
127 protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
128 protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
129 protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
130 protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
131 protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
132
133
134 protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
135
136 private static boolean INITIALIZED = false;
137
138 private static int DEFAULT_LOG_LEVEL = LOG_LEVEL_INFO;
139 private static boolean SHOW_DATE_TIME = false;
140 private static String DATE_TIME_FORMAT_STR = null;
141 private static DateFormat DATE_FORMATTER = null;
142 private static boolean SHOW_THREAD_NAME = true;
143 private static boolean SHOW_LOG_NAME = true;
144 private static boolean SHOW_SHORT_LOG_NAME = false;
145 private static String LOG_FILE = "System.err";
146 private static PrintStream TARGET_STREAM = null;
147 private static boolean LEVEL_IN_BRACKETS = false;
148 private static String WARN_LEVEL_STRING = "WARN";
149
150
151 public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
152
153 public static final String DEFAULT_LOG_LEVEL_KEY = SYSTEM_PREFIX + "defaultLogLevel";
154 public static final String SHOW_DATE_TIME_KEY = SYSTEM_PREFIX + "showDateTime";
155 public static final String DATE_TIME_FORMAT_KEY = SYSTEM_PREFIX + "dateTimeFormat";
156 public static final String SHOW_THREAD_NAME_KEY = SYSTEM_PREFIX + "showThreadName";
157 public static final String SHOW_LOG_NAME_KEY = SYSTEM_PREFIX + "showLogName";
158 public static final String SHOW_SHORT_LOG_NAME_KEY = SYSTEM_PREFIX + "showShortLogName";
159 public static final String LOG_FILE_KEY = SYSTEM_PREFIX + "logFile";
160 public static final String LEVEL_IN_BRACKETS_KEY = SYSTEM_PREFIX + "levelInBrackets";
161 public static final String WARN_LEVEL_STRING_KEY = SYSTEM_PREFIX + "warnLevelString";
162
163 public static final String LOG_KEY_PREFIX = SYSTEM_PREFIX + "log.";
164
165 private static String getStringProperty(String name) {
166 String prop = null;
167 try {
168 prop = System.getProperty(name);
169 } catch (SecurityException e) {
170 ;
171 }
172 return (prop == null) ? SIMPLE_LOGGER_PROPS.getProperty(name) : prop;
173 }
174
175 private static String getStringProperty(String name, String defaultValue) {
176 String prop = getStringProperty(name);
177 return (prop == null) ? defaultValue : prop;
178 }
179
180 private static boolean getBooleanProperty(String name, boolean defaultValue) {
181 String prop = getStringProperty(name);
182 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
183 }
184
185 static void lazyInit() {
186 if (INITIALIZED) {
187 return;
188 }
189 INITIALIZED = true;
190 init();
191 }
192
193 static void init() {
194 loadProperties();
195
196 String defaultLogLevelString = getStringProperty(DEFAULT_LOG_LEVEL_KEY, null);
197 if (defaultLogLevelString != null)
198 DEFAULT_LOG_LEVEL = stringToLevel(defaultLogLevelString);
199
200 SHOW_LOG_NAME = getBooleanProperty(SHOW_LOG_NAME_KEY, SHOW_LOG_NAME);
201 SHOW_SHORT_LOG_NAME = getBooleanProperty(SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME);
202 SHOW_DATE_TIME = getBooleanProperty(SHOW_DATE_TIME_KEY, SHOW_DATE_TIME);
203 SHOW_THREAD_NAME = getBooleanProperty(SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME);
204 DATE_TIME_FORMAT_STR = getStringProperty(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR);
205 LEVEL_IN_BRACKETS = getBooleanProperty(LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS);
206 WARN_LEVEL_STRING = getStringProperty(WARN_LEVEL_STRING_KEY, WARN_LEVEL_STRING);
207
208 LOG_FILE = getStringProperty(LOG_FILE_KEY, LOG_FILE);
209 TARGET_STREAM = computeTargetStream(LOG_FILE);
210
211 if (DATE_TIME_FORMAT_STR != null) {
212 try {
213 DATE_FORMATTER = new SimpleDateFormat(DATE_TIME_FORMAT_STR);
214 } catch (IllegalArgumentException e) {
215 Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
216 }
217 }
218 }
219
220 private static PrintStream computeTargetStream(String logFile) {
221 if ("System.err".equalsIgnoreCase(logFile))
222 return System.err;
223 else if ("System.out".equalsIgnoreCase(logFile)) {
224 return System.out;
225 } else {
226 try {
227 FileOutputStream fos = new FileOutputStream(logFile);
228 PrintStream printStream = new PrintStream(fos);
229 return printStream;
230 } catch (FileNotFoundException e) {
231 Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
232 return System.err;
233 }
234 }
235 }
236
237 private static void loadProperties() {
238
239 InputStream in = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
240 public InputStream run() {
241 ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
242 if (threadCL != null) {
243 return threadCL.getResourceAsStream(CONFIGURATION_FILE);
244 } else {
245 return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
246 }
247 }
248 });
249 if (null != in) {
250 try {
251 SIMPLE_LOGGER_PROPS.load(in);
252 } catch (java.io.IOException e) {
253
254 } finally {
255 try {
256 in.close();
257 } catch (java.io.IOException e) {
258
259 }
260 }
261 }
262 }
263
264
265 protected int currentLogLevel = LOG_LEVEL_INFO;
266
267 private transient String shortLogName = null;
268
269
270
271
272
273 SimpleLogger(String name) {
274 this.name = name;
275
276 String levelString = recursivelyComputeLevelString();
277 if (levelString != null) {
278 this.currentLogLevel = stringToLevel(levelString);
279 } else {
280 this.currentLogLevel = DEFAULT_LOG_LEVEL;
281 }
282 }
283
284 String recursivelyComputeLevelString() {
285 String tempName = name;
286 String levelString = null;
287 int indexOfLastDot = tempName.length();
288 while ((levelString == null) && (indexOfLastDot > -1)) {
289 tempName = tempName.substring(0, indexOfLastDot);
290 levelString = getStringProperty(LOG_KEY_PREFIX + tempName, null);
291 indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
292 }
293 return levelString;
294 }
295
296 private static int stringToLevel(String levelStr) {
297 if ("trace".equalsIgnoreCase(levelStr)) {
298 return LOG_LEVEL_TRACE;
299 } else if ("debug".equalsIgnoreCase(levelStr)) {
300 return LOG_LEVEL_DEBUG;
301 } else if ("info".equalsIgnoreCase(levelStr)) {
302 return LOG_LEVEL_INFO;
303 } else if ("warn".equalsIgnoreCase(levelStr)) {
304 return LOG_LEVEL_WARN;
305 } else if ("error".equalsIgnoreCase(levelStr)) {
306 return LOG_LEVEL_ERROR;
307 } else if ("off".equalsIgnoreCase(levelStr)) {
308 return LOG_LEVEL_OFF;
309 }
310
311 return LOG_LEVEL_INFO;
312 }
313
314
315
316
317
318
319
320
321
322 private void log(int level, String message, Throwable t) {
323 if (!isLevelEnabled(level)) {
324 return;
325 }
326
327 StringBuilder buf = new StringBuilder(32);
328
329
330 if (SHOW_DATE_TIME) {
331 if (DATE_FORMATTER != null) {
332 buf.append(getFormattedDate());
333 buf.append(' ');
334 } else {
335 buf.append(System.currentTimeMillis() - START_TIME);
336 buf.append(' ');
337 }
338 }
339
340
341 if (SHOW_THREAD_NAME) {
342 buf.append('[');
343 buf.append(Thread.currentThread().getName());
344 buf.append("] ");
345 }
346
347 if (LEVEL_IN_BRACKETS)
348 buf.append('[');
349
350
351 buf.append(renderLevel(level));
352 if (LEVEL_IN_BRACKETS)
353 buf.append(']');
354 buf.append(' ');
355
356
357 if (SHOW_SHORT_LOG_NAME) {
358 if (shortLogName == null)
359 shortLogName = computeShortName();
360 buf.append(String.valueOf(shortLogName)).append(" - ");
361 } else if (SHOW_LOG_NAME) {
362 buf.append(String.valueOf(name)).append(" - ");
363 }
364
365
366 buf.append(message);
367
368 write(buf, t);
369
370 }
371
372 void write(StringBuilder buf, Throwable t) {
373 TARGET_STREAM.println(buf.toString());
374 if (t != null) {
375 t.printStackTrace(TARGET_STREAM);
376 }
377 TARGET_STREAM.flush();
378 }
379
380 private String getFormattedDate() {
381 Date now = new Date();
382 String dateText;
383 synchronized (DATE_FORMATTER) {
384 dateText = DATE_FORMATTER.format(now);
385 }
386 return dateText;
387 }
388
389 private String computeShortName() {
390 return name.substring(name.lastIndexOf(".") + 1);
391 }
392
393
394
395
396
397
398
399
400
401 private void formatAndLog(int level, String format, Object arg1, Object arg2) {
402 if (!isLevelEnabled(level)) {
403 return;
404 }
405 FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
406 log(level, tp.getMessage(), tp.getThrowable());
407 }
408
409
410
411
412
413
414
415
416 private void formatAndLog(int level, String format, Object... arguments) {
417 if (!isLevelEnabled(level)) {
418 return;
419 }
420 FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
421 log(level, tp.getMessage(), tp.getThrowable());
422 }
423
424
425
426
427
428
429 protected boolean isLevelEnabled(int logLevel) {
430
431
432 return (logLevel >= currentLogLevel);
433 }
434
435
436 public boolean isTraceEnabled() {
437 return isLevelEnabled(LOG_LEVEL_TRACE);
438 }
439
440
441
442
443
444 public void trace(String msg) {
445 log(LOG_LEVEL_TRACE, msg, null);
446 }
447
448
449
450
451
452 public void trace(String format, Object param1) {
453 formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
454 }
455
456
457
458
459
460 public void trace(String format, Object param1, Object param2) {
461 formatAndLog(LOG_LEVEL_TRACE, format, param1, param2);
462 }
463
464
465
466
467
468 public void trace(String format, Object... argArray) {
469 formatAndLog(LOG_LEVEL_TRACE, format, argArray);
470 }
471
472
473 public void trace(String msg, Throwable t) {
474 log(LOG_LEVEL_TRACE, msg, t);
475 }
476
477
478 public boolean isDebugEnabled() {
479 return isLevelEnabled(LOG_LEVEL_DEBUG);
480 }
481
482
483
484
485
486 public void debug(String msg) {
487 log(LOG_LEVEL_DEBUG, msg, null);
488 }
489
490
491
492
493
494 public void debug(String format, Object param1) {
495 formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
496 }
497
498
499
500
501
502 public void debug(String format, Object param1, Object param2) {
503 formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2);
504 }
505
506
507
508
509
510 public void debug(String format, Object... argArray) {
511 formatAndLog(LOG_LEVEL_DEBUG, format, argArray);
512 }
513
514
515 public void debug(String msg, Throwable t) {
516 log(LOG_LEVEL_DEBUG, msg, t);
517 }
518
519
520 public boolean isInfoEnabled() {
521 return isLevelEnabled(LOG_LEVEL_INFO);
522 }
523
524
525
526
527
528 public void info(String msg) {
529 log(LOG_LEVEL_INFO, msg, null);
530 }
531
532
533
534
535
536 public void info(String format, Object arg) {
537 formatAndLog(LOG_LEVEL_INFO, format, arg, null);
538 }
539
540
541
542
543
544 public void info(String format, Object arg1, Object arg2) {
545 formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2);
546 }
547
548
549
550
551
552 public void info(String format, Object... argArray) {
553 formatAndLog(LOG_LEVEL_INFO, format, argArray);
554 }
555
556
557 public void info(String msg, Throwable t) {
558 log(LOG_LEVEL_INFO, msg, t);
559 }
560
561
562 public boolean isWarnEnabled() {
563 return isLevelEnabled(LOG_LEVEL_WARN);
564 }
565
566
567
568
569
570 public void warn(String msg) {
571 log(LOG_LEVEL_WARN, msg, null);
572 }
573
574
575
576
577
578 public void warn(String format, Object arg) {
579 formatAndLog(LOG_LEVEL_WARN, format, arg, null);
580 }
581
582
583
584
585
586 public void warn(String format, Object arg1, Object arg2) {
587 formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2);
588 }
589
590
591
592
593
594 public void warn(String format, Object... argArray) {
595 formatAndLog(LOG_LEVEL_WARN, format, argArray);
596 }
597
598
599 public void warn(String msg, Throwable t) {
600 log(LOG_LEVEL_WARN, msg, t);
601 }
602
603
604 public boolean isErrorEnabled() {
605 return isLevelEnabled(LOG_LEVEL_ERROR);
606 }
607
608
609
610
611
612 public void error(String msg) {
613 log(LOG_LEVEL_ERROR, msg, null);
614 }
615
616
617
618
619
620 public void error(String format, Object arg) {
621 formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
622 }
623
624
625
626
627
628 public void error(String format, Object arg1, Object arg2) {
629 formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2);
630 }
631
632
633
634
635
636 public void error(String format, Object... argArray) {
637 formatAndLog(LOG_LEVEL_ERROR, format, argArray);
638 }
639
640
641 public void error(String msg, Throwable t) {
642 log(LOG_LEVEL_ERROR, msg, t);
643 }
644
645 public void log(LoggingEvent event) {
646 int levelInt = event.getLevel().toInt();
647
648 if (!isLevelEnabled(levelInt)) {
649 return;
650 }
651 FormattingTuple tp = MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray(), event.getThrowable());
652 log(levelInt, tp.getMessage(), event.getThrowable());
653 }
654
655 protected void renderThrowable(Throwable t, PrintStream stream) {}
656 protected String renderLevel(int level) { return ""; }
657 }