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.util.logging.Level;
28 import java.util.logging.LogRecord;
29
30 import org.slf4j.Logger;
31 import org.slf4j.Marker;
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 public final class JDK14LoggerAdapter extends MarkerIgnoringBase implements
47 LocationAwareLogger {
48
49 private static final long serialVersionUID = -8053026990503422791L;
50
51 final java.util.logging.Logger logger;
52
53
54
55 JDK14LoggerAdapter(java.util.logging.Logger logger) {
56 this.logger = logger;
57 this.name = logger.getName();
58 }
59
60
61
62
63
64
65 public boolean isTraceEnabled() {
66 return logger.isLoggable(Level.FINEST);
67 }
68
69
70
71
72
73
74
75 public void trace(String msg) {
76 if (logger.isLoggable(Level.FINEST)) {
77 log(SELF, Level.FINEST, msg, null);
78 }
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public void trace(String format, Object arg) {
96 if (logger.isLoggable(Level.FINEST)) {
97 FormattingTuple ft = MessageFormatter.format(format, arg);
98 log(SELF, Level.FINEST, ft.getMessage(), ft.getThrowable());
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public void trace(String format, Object arg1, Object arg2) {
119 if (logger.isLoggable(Level.FINEST)) {
120 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
121 log(SELF, Level.FINEST, ft.getMessage(), ft.getThrowable());
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public void trace(String format, Object[] argArray) {
140 if (logger.isLoggable(Level.FINEST)) {
141 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
142 log(SELF, Level.FINEST, ft.getMessage(), ft.getThrowable());
143 }
144 }
145
146
147
148
149
150
151
152
153
154 public void trace(String msg, Throwable t) {
155 if (logger.isLoggable(Level.FINEST)) {
156 log(SELF, Level.FINEST, msg, t);
157 }
158 }
159
160
161
162
163
164
165 public boolean isDebugEnabled() {
166 return logger.isLoggable(Level.FINE);
167 }
168
169
170
171
172
173
174
175 public void debug(String msg) {
176 if (logger.isLoggable(Level.FINE)) {
177 log(SELF, Level.FINE, msg, null);
178 }
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public void debug(String format, Object arg) {
195 if (logger.isLoggable(Level.FINE)) {
196 FormattingTuple ft = MessageFormatter.format(format, arg);
197 log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public void debug(String format, Object arg1, Object arg2) {
218 if (logger.isLoggable(Level.FINE)) {
219 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
220 log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
221 }
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public void debug(String format, Object[] argArray) {
239 if (logger.isLoggable(Level.FINE)) {
240 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
241 log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
242 }
243 }
244
245
246
247
248
249
250
251
252
253 public void debug(String msg, Throwable t) {
254 if (logger.isLoggable(Level.FINE)) {
255 log(SELF, Level.FINE, msg, t);
256 }
257 }
258
259
260
261
262
263
264 public boolean isInfoEnabled() {
265 return logger.isLoggable(Level.INFO);
266 }
267
268
269
270
271
272
273
274 public void info(String msg) {
275 if (logger.isLoggable(Level.INFO)) {
276 log(SELF, Level.INFO, msg, null);
277 }
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public void info(String format, Object arg) {
294 if (logger.isLoggable(Level.INFO)) {
295 FormattingTuple ft = MessageFormatter.format(format, arg);
296 log(SELF, Level.INFO, ft.getMessage(), ft.getThrowable());
297 }
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public void info(String format, Object arg1, Object arg2) {
317 if (logger.isLoggable(Level.INFO)) {
318 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
319 log(SELF, Level.INFO, ft.getMessage(), ft.getThrowable());
320 }
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 public void info(String format, Object[] argArray) {
338 if (logger.isLoggable(Level.INFO)) {
339 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
340 log(SELF, Level.INFO, ft.getMessage(), ft.getThrowable());
341 }
342 }
343
344
345
346
347
348
349
350
351
352
353 public void info(String msg, Throwable t) {
354 if (logger.isLoggable(Level.INFO)) {
355 log(SELF, Level.INFO, msg, t);
356 }
357 }
358
359
360
361
362
363
364
365 public boolean isWarnEnabled() {
366 return logger.isLoggable(Level.WARNING);
367 }
368
369
370
371
372
373
374
375 public void warn(String msg) {
376 if (logger.isLoggable(Level.WARNING)) {
377 log(SELF, Level.WARNING, msg, null);
378 }
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 public void warn(String format, Object arg) {
396 if (logger.isLoggable(Level.WARNING)) {
397 FormattingTuple ft = MessageFormatter.format(format, arg);
398 log(SELF, Level.WARNING, ft.getMessage(), ft.getThrowable());
399 }
400 }
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 public void warn(String format, Object arg1, Object arg2) {
419 if (logger.isLoggable(Level.WARNING)) {
420 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
421 log(SELF, Level.WARNING, ft.getMessage(), ft.getThrowable());
422 }
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 public void warn(String format, Object[] argArray) {
440 if (logger.isLoggable(Level.WARNING)) {
441 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
442 log(SELF, Level.WARNING, ft.getMessage(), ft.getThrowable());
443 }
444 }
445
446
447
448
449
450
451
452
453
454
455 public void warn(String msg, Throwable t) {
456 if (logger.isLoggable(Level.WARNING)) {
457 log(SELF, Level.WARNING, msg, t);
458 }
459 }
460
461
462
463
464
465
466 public boolean isErrorEnabled() {
467 return logger.isLoggable(Level.SEVERE);
468 }
469
470
471
472
473
474
475
476 public void error(String msg) {
477 if (logger.isLoggable(Level.SEVERE)) {
478 log(SELF, Level.SEVERE, msg, null);
479 }
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496 public void error(String format, Object arg) {
497 if (logger.isLoggable(Level.SEVERE)) {
498 FormattingTuple ft = MessageFormatter.format(format, arg);
499 log(SELF, Level.SEVERE, ft.getMessage(), ft.getThrowable());
500 }
501 }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519 public void error(String format, Object arg1, Object arg2) {
520 if (logger.isLoggable(Level.SEVERE)) {
521 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
522 log(SELF, Level.SEVERE, ft.getMessage(), ft.getThrowable());
523 }
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 public void error(String format, Object[] argArray) {
541 if (logger.isLoggable(Level.SEVERE)) {
542 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
543 log(SELF, Level.SEVERE, ft.getMessage(), ft.getThrowable());
544 }
545 }
546
547
548
549
550
551
552
553
554
555
556 public void error(String msg, Throwable t) {
557 if (logger.isLoggable(Level.SEVERE)) {
558 log(SELF, Level.SEVERE, msg, t);
559 }
560 }
561
562
563
564
565
566
567
568
569
570
571
572
573 private void log(String callerFQCN, Level level, String msg, Throwable t) {
574
575 LogRecord record = new LogRecord(level, msg);
576 record.setLoggerName(getName());
577 record.setThrown(t);
578 fillCallerData(callerFQCN, record);
579 logger.log(record);
580
581 }
582
583 static String SELF = JDK14LoggerAdapter.class.getName();
584 static String SUPER = MarkerIgnoringBase.class.getName();
585
586
587
588
589
590
591
592 final private void fillCallerData(String callerFQCN, LogRecord record) {
593 StackTraceElement[] steArray = new Throwable().getStackTrace();
594
595 int selfIndex = -1;
596 for (int i = 0; i < steArray.length; i++) {
597 final String className = steArray[i].getClassName();
598 if (className.equals(callerFQCN) || className.equals(SUPER)) {
599 selfIndex = i;
600 break;
601 }
602 }
603
604 int found = -1;
605 for (int i = selfIndex + 1; i < steArray.length; i++) {
606 final String className = steArray[i].getClassName();
607 if (!(className.equals(callerFQCN) || className.equals(SUPER))) {
608 found = i;
609 break;
610 }
611 }
612
613 if (found != -1) {
614 StackTraceElement ste = steArray[found];
615
616
617 record.setSourceClassName(ste.getClassName());
618 record.setSourceMethodName(ste.getMethodName());
619 }
620 }
621
622 public void log(Marker marker, String callerFQCN, int level, String message,
623 Object[] argArray, Throwable t) {
624 Level julLevel;
625 switch (level) {
626 case LocationAwareLogger.TRACE_INT:
627 julLevel = Level.FINEST;
628 break;
629 case LocationAwareLogger.DEBUG_INT:
630 julLevel = Level.FINE;
631 break;
632 case LocationAwareLogger.INFO_INT:
633 julLevel = Level.INFO;
634 break;
635 case LocationAwareLogger.WARN_INT:
636 julLevel = Level.WARNING;
637 break;
638 case LocationAwareLogger.ERROR_INT:
639 julLevel = Level.SEVERE;
640 break;
641 default:
642 throw new IllegalStateException("Level number " + level
643 + " is not recognized.");
644 }
645
646
647
648
649
650 if (logger.isLoggable(julLevel)) {
651 log(callerFQCN, julLevel, message, t);
652 }
653 }
654 }