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