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