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.PrintStream;
28 import java.util.Date;
29
30 import org.slf4j.Logger;
31 import org.slf4j.event.LoggingEvent;
32 import org.slf4j.helpers.FormattingTuple;
33 import org.slf4j.helpers.MarkerIgnoringBase;
34 import org.slf4j.helpers.MessageFormatter;
35 import org.slf4j.spi.LocationAwareLogger;
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public class SimpleLogger extends MarkerIgnoringBase {
144
145 private static final long serialVersionUID = -632788891211436180L;
146
147 private static long START_TIME = System.currentTimeMillis();
148
149 protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
150 protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
151 protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
152 protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
153 protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
154
155
156
157 protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
158
159 private static boolean INITIALIZED = false;
160 static SimpleLoggerConfiguration CONFIG_PARAMS = null;
161
162 static void lazyInit() {
163 if (INITIALIZED) {
164 return;
165 }
166 INITIALIZED = true;
167 init();
168 }
169
170
171
172 static void init() {
173 CONFIG_PARAMS = new SimpleLoggerConfiguration();
174 CONFIG_PARAMS.init();
175 }
176
177
178 protected int currentLogLevel = LOG_LEVEL_INFO;
179
180 private transient String shortLogName = null;
181
182
183
184
185
186 public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
187
188 public static final String LOG_KEY_PREFIX = SimpleLogger.SYSTEM_PREFIX + "log.";
189
190 public static final String CACHE_OUTPUT_STREAM_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "cacheOutputStream";
191
192 public static final String WARN_LEVEL_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "warnLevelString";
193
194 public static final String LEVEL_IN_BRACKETS_KEY = SimpleLogger.SYSTEM_PREFIX + "levelInBrackets";
195
196 public static final String LOG_FILE_KEY = SimpleLogger.SYSTEM_PREFIX + "logFile";
197
198 public static final String SHOW_SHORT_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showShortLogName";
199
200 public static final String SHOW_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showLogName";
201
202 public static final String SHOW_THREAD_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadName";
203
204 public static final String DATE_TIME_FORMAT_KEY = SimpleLogger.SYSTEM_PREFIX + "dateTimeFormat";
205
206 public static final String SHOW_DATE_TIME_KEY = SimpleLogger.SYSTEM_PREFIX + "showDateTime";
207
208 public static final String DEFAULT_LOG_LEVEL_KEY = SimpleLogger.SYSTEM_PREFIX + "defaultLogLevel";
209
210
211
212
213
214 SimpleLogger(String name) {
215 this.name = name;
216
217 String levelString = recursivelyComputeLevelString();
218 if (levelString != null) {
219 this.currentLogLevel = SimpleLoggerConfiguration.stringToLevel(levelString);
220 } else {
221 this.currentLogLevel = CONFIG_PARAMS.defaultLogLevel;
222 }
223 }
224
225 String recursivelyComputeLevelString() {
226 String tempName = name;
227 String levelString = null;
228 int indexOfLastDot = tempName.length();
229 while ((levelString == null) && (indexOfLastDot > -1)) {
230 tempName = tempName.substring(0, indexOfLastDot);
231 levelString = CONFIG_PARAMS.getStringProperty(SimpleLogger.LOG_KEY_PREFIX + tempName, null);
232 indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
233 }
234 return levelString;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 private void log(int level, String message, Throwable t) {
249 if (!isLevelEnabled(level)) {
250 return;
251 }
252
253 StringBuilder buf = new StringBuilder(32);
254
255
256 if (CONFIG_PARAMS.showDateTime) {
257 if (CONFIG_PARAMS.dateFormatter != null) {
258 buf.append(getFormattedDate());
259 buf.append(' ');
260 } else {
261 buf.append(System.currentTimeMillis() - START_TIME);
262 buf.append(' ');
263 }
264 }
265
266
267 if (CONFIG_PARAMS.showThreadName) {
268 buf.append('[');
269 buf.append(Thread.currentThread().getName());
270 buf.append("] ");
271 }
272
273 if (CONFIG_PARAMS.levelInBrackets)
274 buf.append('[');
275
276
277 String levelStr = renderLevel(level);
278 buf.append(levelStr);
279 if (CONFIG_PARAMS.levelInBrackets)
280 buf.append(']');
281 buf.append(' ');
282
283
284 if (CONFIG_PARAMS.showShortLogName) {
285 if (shortLogName == null)
286 shortLogName = computeShortName();
287 buf.append(String.valueOf(shortLogName)).append(" - ");
288 } else if (CONFIG_PARAMS.showLogName) {
289 buf.append(String.valueOf(name)).append(" - ");
290 }
291
292
293 buf.append(message);
294
295 write(buf, t);
296
297 }
298
299 protected String renderLevel(int level) {
300 switch (level) {
301 case LOG_LEVEL_TRACE:
302 return "TRACE";
303 case LOG_LEVEL_DEBUG:
304 return ("DEBUG");
305 case LOG_LEVEL_INFO:
306 return "INFO";
307 case LOG_LEVEL_WARN:
308 return CONFIG_PARAMS.warnLevelString;
309 case LOG_LEVEL_ERROR:
310 return "ERROR";
311 }
312 throw new IllegalStateException("Unrecognized level [" + level + "]");
313 }
314
315 void write(StringBuilder buf, Throwable t) {
316 PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
317
318 targetStream.println(buf.toString());
319 writeThrowable(t, targetStream);
320 targetStream.flush();
321 }
322
323 protected void writeThrowable(Throwable t, PrintStream targetStream) {
324 if (t != null) {
325 t.printStackTrace(targetStream);
326 }
327 }
328
329 private String getFormattedDate() {
330 Date now = new Date();
331 String dateText;
332 synchronized (CONFIG_PARAMS.dateFormatter) {
333 dateText = CONFIG_PARAMS.dateFormatter.format(now);
334 }
335 return dateText;
336 }
337
338 private String computeShortName() {
339 return name.substring(name.lastIndexOf(".") + 1);
340 }
341
342
343
344
345
346
347
348
349
350 private void formatAndLog(int level, String format, Object arg1, Object arg2) {
351 if (!isLevelEnabled(level)) {
352 return;
353 }
354 FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
355 log(level, tp.getMessage(), tp.getThrowable());
356 }
357
358
359
360
361
362
363
364
365
366 private void formatAndLog(int level, String format, Object... arguments) {
367 if (!isLevelEnabled(level)) {
368 return;
369 }
370 FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
371 log(level, tp.getMessage(), tp.getThrowable());
372 }
373
374
375
376
377
378
379
380 protected boolean isLevelEnabled(int logLevel) {
381
382
383 return (logLevel >= currentLogLevel);
384 }
385
386
387 public boolean isTraceEnabled() {
388 return isLevelEnabled(LOG_LEVEL_TRACE);
389 }
390
391
392
393
394
395 public void trace(String msg) {
396 log(LOG_LEVEL_TRACE, msg, null);
397 }
398
399
400
401
402
403 public void trace(String format, Object param1) {
404 formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
405 }
406
407
408
409
410
411 public void trace(String format, Object param1, Object param2) {
412 formatAndLog(LOG_LEVEL_TRACE, format, param1, param2);
413 }
414
415
416
417
418
419 public void trace(String format, Object... argArray) {
420 formatAndLog(LOG_LEVEL_TRACE, format, argArray);
421 }
422
423
424 public void trace(String msg, Throwable t) {
425 log(LOG_LEVEL_TRACE, msg, t);
426 }
427
428
429 public boolean isDebugEnabled() {
430 return isLevelEnabled(LOG_LEVEL_DEBUG);
431 }
432
433
434
435
436
437 public void debug(String msg) {
438 log(LOG_LEVEL_DEBUG, msg, null);
439 }
440
441
442
443
444
445 public void debug(String format, Object param1) {
446 formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
447 }
448
449
450
451
452
453 public void debug(String format, Object param1, Object param2) {
454 formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2);
455 }
456
457
458
459
460
461 public void debug(String format, Object... argArray) {
462 formatAndLog(LOG_LEVEL_DEBUG, format, argArray);
463 }
464
465
466 public void debug(String msg, Throwable t) {
467 log(LOG_LEVEL_DEBUG, msg, t);
468 }
469
470
471 public boolean isInfoEnabled() {
472 return isLevelEnabled(LOG_LEVEL_INFO);
473 }
474
475
476
477
478
479 public void info(String msg) {
480 log(LOG_LEVEL_INFO, msg, null);
481 }
482
483
484
485
486
487 public void info(String format, Object arg) {
488 formatAndLog(LOG_LEVEL_INFO, format, arg, null);
489 }
490
491
492
493
494
495 public void info(String format, Object arg1, Object arg2) {
496 formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2);
497 }
498
499
500
501
502
503 public void info(String format, Object... argArray) {
504 formatAndLog(LOG_LEVEL_INFO, format, argArray);
505 }
506
507
508 public void info(String msg, Throwable t) {
509 log(LOG_LEVEL_INFO, msg, t);
510 }
511
512
513 public boolean isWarnEnabled() {
514 return isLevelEnabled(LOG_LEVEL_WARN);
515 }
516
517
518
519
520
521 public void warn(String msg) {
522 log(LOG_LEVEL_WARN, msg, null);
523 }
524
525
526
527
528
529 public void warn(String format, Object arg) {
530 formatAndLog(LOG_LEVEL_WARN, format, arg, null);
531 }
532
533
534
535
536
537 public void warn(String format, Object arg1, Object arg2) {
538 formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2);
539 }
540
541
542
543
544
545 public void warn(String format, Object... argArray) {
546 formatAndLog(LOG_LEVEL_WARN, format, argArray);
547 }
548
549
550 public void warn(String msg, Throwable t) {
551 log(LOG_LEVEL_WARN, msg, t);
552 }
553
554
555 public boolean isErrorEnabled() {
556 return isLevelEnabled(LOG_LEVEL_ERROR);
557 }
558
559
560
561
562
563 public void error(String msg) {
564 log(LOG_LEVEL_ERROR, msg, null);
565 }
566
567
568
569
570
571 public void error(String format, Object arg) {
572 formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
573 }
574
575
576
577
578
579 public void error(String format, Object arg1, Object arg2) {
580 formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2);
581 }
582
583
584
585
586
587 public void error(String format, Object... argArray) {
588 formatAndLog(LOG_LEVEL_ERROR, format, argArray);
589 }
590
591
592 public void error(String msg, Throwable t) {
593 log(LOG_LEVEL_ERROR, msg, t);
594 }
595
596 public void log(LoggingEvent event) {
597 int levelInt = event.getLevel().toInt();
598
599 if (!isLevelEnabled(levelInt)) {
600 return;
601 }
602 FormattingTuple tp = MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray(), event.getThrowable());
603 log(levelInt, tp.getMessage(), event.getThrowable());
604 }
605
606 }