1 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.MarkerIgnoringBase; 42 import org.slf4j.helpers.MessageFormatter; 43 import org.slf4j.spi.LocationAwareLogger; 44 45 54 public final class JDK14LoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger { 55 final java.util.logging.Logger logger; 56 57 JDK14LoggerAdapter(java.util.logging.Logger logger) { 60 this.logger = logger; 61 } 62 63 public String getName() { 64 return logger.getName(); 65 } 66 67 72 public boolean isTraceEnabled() { 73 return logger.isLoggable(Level.FINEST); 74 } 75 76 82 public void trace(String msg) { 83 log(SELF, Level.FINEST, msg, null); 84 } 85 86 99 public void trace(String format, Object arg) { 100 if (logger.isLoggable(Level.FINEST)) { 101 String msgStr = MessageFormatter.format(format, arg); 102 log(SELF, Level.FINEST, msgStr, null); 103 } 104 } 105 106 122 public void trace(String format, Object arg1, Object arg2) { 123 if (logger.isLoggable(Level.FINEST)) { 124 String msgStr = MessageFormatter.format(format, arg1, arg2); 125 log(SELF, Level.FINEST, msgStr, null); 126 } 127 } 128 129 143 public void trace(String format, Object [] argArray) { 144 if (logger.isLoggable(Level.FINEST)) { 145 String msgStr = MessageFormatter.arrayFormat(format, argArray); 146 log(SELF, Level.FINEST, msgStr, null); 147 } 148 } 149 150 158 public void trace(String msg, Throwable t) { 159 log(SELF, Level.FINEST, msg, t); 160 } 161 162 163 168 public boolean isDebugEnabled() { 169 return logger.isLoggable(Level.FINE); 170 } 171 172 178 public void debug(String msg) { 179 log(SELF, Level.FINE, msg, null); 180 } 181 182 195 public void debug(String format, Object arg) { 196 if (logger.isLoggable(Level.FINE)) { 197 String msgStr = MessageFormatter.format(format, arg); 198 log(SELF, Level.FINE, msgStr, null); 199 } 200 } 201 202 218 public void debug(String format, Object arg1, Object arg2) { 219 if (logger.isLoggable(Level.FINE)) { 220 String msgStr = MessageFormatter.format(format, arg1, arg2); 221 log(SELF, Level.FINE, msgStr, null); 222 } 223 } 224 225 239 public void debug(String format, Object [] argArray) { 240 if (logger.isLoggable(Level.FINE)) { 241 String msgStr = MessageFormatter.arrayFormat(format, argArray); 242 log(SELF, Level.FINE, msgStr, null); 243 } 244 } 245 246 254 public void debug(String msg, Throwable t) { 255 log(SELF, Level.FINE, msg, t); 256 } 257 258 263 public boolean isInfoEnabled() { 264 return logger.isLoggable(Level.INFO); 265 } 266 267 273 public void info(String msg) { 274 log(SELF, Level.INFO, msg, null); 275 } 276 277 290 public void info(String format, Object arg) { 291 if (logger.isLoggable(Level.INFO)) { 292 String msgStr = MessageFormatter.format(format, arg); 293 log(SELF, Level.INFO, msgStr, null); 294 } 295 } 296 297 313 public void info(String format, Object arg1, Object arg2) { 314 if (logger.isLoggable(Level.INFO)) { 315 String msgStr = MessageFormatter.format(format, arg1, arg2); 316 log(SELF, Level.INFO, msgStr, null); 317 } 318 } 319 320 334 public void info(String format, Object [] argArray) { 335 if (logger.isLoggable(Level.INFO)) { 336 String msgStr = MessageFormatter.arrayFormat(format, argArray); 337 log(SELF, Level.INFO, msgStr, null); 338 } 339 } 340 341 350 public void info(String msg, Throwable t) { 351 log(SELF, Level.INFO, msg, t); 352 } 353 354 360 public boolean isWarnEnabled() { 361 return logger.isLoggable(Level.WARNING); 362 } 363 364 370 public void warn(String msg) { 371 log(SELF, Level.WARNING, msg, null); 372 } 373 374 388 public void warn(String format, Object arg) { 389 if (logger.isLoggable(Level.WARNING)) { 390 String msgStr = MessageFormatter.format(format, arg); 391 log(SELF, Level.WARNING, msgStr, null); 392 } 393 } 394 395 411 public void warn(String format, Object arg1, Object arg2) { 412 if (logger.isLoggable(Level.WARNING)) { 413 String msgStr = MessageFormatter.format(format, arg1, arg2); 414 log(SELF, Level.WARNING, msgStr, null); 415 } 416 } 417 418 432 public void warn(String format, Object [] argArray) { 433 if (logger.isLoggable(Level.WARNING)) { 434 String msgStr = MessageFormatter.arrayFormat(format, argArray); 435 log(SELF, Level.WARNING, msgStr, null); 436 } 437 } 438 439 448 public void warn(String msg, Throwable t) { 449 log(SELF, Level.WARNING, msg, t); 450 } 451 452 457 public boolean isErrorEnabled() { 458 return logger.isLoggable(Level.SEVERE); 459 } 460 461 467 public void error(String msg) { 468 log(SELF, Level.SEVERE, msg, null); 469 } 470 471 485 public void error(String format, Object arg) { 486 if (logger.isLoggable(Level.SEVERE)) { 487 String msgStr = MessageFormatter.format(format, arg); 488 log(SELF, Level.SEVERE, msgStr, null); 489 } 490 } 491 492 508 public void error(String format, Object arg1, Object arg2) { 509 if (logger.isLoggable(Level.SEVERE)) { 510 String msgStr = MessageFormatter.format(format, arg1, arg2); 511 log(SELF, Level.SEVERE, msgStr, null); 512 } 513 } 514 515 529 public void error(String format, Object [] argArray) { 530 if (logger.isLoggable(Level.SEVERE)) { 531 String msgStr = MessageFormatter.arrayFormat(format, argArray); 532 log(SELF, Level.SEVERE, msgStr, null); 533 } 534 } 535 536 545 public void error(String msg, Throwable t) { 546 log(SELF, Level.SEVERE, msg, t); 547 } 548 549 550 560 private void log(String callerFQCN, Level level, String msg, Throwable t) { 561 LogRecord record = new LogRecord (level, msg); 563 record.setLoggerName(getName()); 564 record.setThrown(t); 565 fillCallerData(callerFQCN, record); 566 logger.log(record); 567 568 } 569 570 static String SELF = JDK14LoggerAdapter.class.getName(); 571 static String SUPER = MarkerIgnoringBase.class.getName(); 572 577 final private void fillCallerData(String callerFQCN, LogRecord record) { 578 StackTraceElement [] steArray = new Throwable ().getStackTrace(); 579 580 int selfIndex = -1; 581 for (int i = 0; i < steArray.length; i++) { 582 final String className = steArray[i].getClassName(); 583 if (className.equals(callerFQCN) || className.equals(SUPER)) { 584 selfIndex = i; 585 break; 586 } 587 } 588 589 int found = -1; 590 for (int i = selfIndex + 1; i < steArray.length; i++) { 591 final String className = steArray[i].getClassName(); 592 if (!(className.equals(callerFQCN) || className.equals(SUPER))) { 593 found = i; 594 break; 595 } 596 } 597 598 if (found != -1) { 599 StackTraceElement ste = steArray[found]; 600 record.setSourceClassName(ste.getClassName()); 603 record.setSourceMethodName(ste.getMethodName()); 604 } 605 } 606 607 public void log(Marker marker, String callerFQCN, int level, String message, Throwable t) { 608 Level julLevel; 609 switch(level) { 610 case LocationAwareLogger.TRACE_INT: 611 julLevel = Level.FINEST; 612 break; 613 case LocationAwareLogger.DEBUG_INT: 614 julLevel = Level.FINE; 615 break; 616 case LocationAwareLogger.INFO_INT: 617 julLevel = Level.INFO; 618 break; 619 case LocationAwareLogger.WARN_INT: 620 julLevel = Level.WARNING; 621 break; 622 case LocationAwareLogger.ERROR_INT: 623 julLevel = Level.SEVERE; 624 break; 625 default: 626 throw new IllegalStateException ("Level number "+level+" is not recognized."); 627 } 628 log(callerFQCN, julLevel, message, t); 629 } 630 } 631 | Popular Tags |