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