1 /* 2 * Copyright (c) 2004-2005 SLF4J.ORG 3 * Copyright (c) 2004-2005 QOS.ch 4 * 5 * All rights reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, and/or sell copies of the Software, and to permit persons 12 * to whom the Software is furnished to do so, provided that the above 13 * copyright notice(s) and this permission notice appear in all copies of 14 * the Software and that both the above copyright notice(s) and this 15 * permission notice appear in supporting documentation. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 20 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 21 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY 22 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER 23 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 24 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 25 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 26 * 27 * Except as contained in this notice, the name of a copyright holder 28 * shall not be used in advertising or otherwise to promote the sale, use 29 * or other dealings in this Software without prior written authorization 30 * of the copyright holder. 31 */ 32 33 34 package org.slf4j.impl; 35 36 import it.imolinfo.jbi4cics.jbi.Messages; 37 import java.text.MessageFormat; 38 import org.apache.log4j.Level; 39 import org.apache.log4j.Logger; 40 import org.slf4j.Marker; 41 import org.slf4j.helpers.MarkerIgnoringBase; 42 import org.slf4j.spi.LocationAwareLogger; 43 44 /** 45 * A wrapper over <code>org.apache.log4j.Logger</code> in conformance with the 46 * <code>org.slf4j.Logger</code> interface. Note that the logging levels 47 * mentioned in this class refer to those defined in the <a 48 * href="http://logging.apache.org/log4j/docs/api/org/apache/log4j/Level.html"> 49 * <code>org.apache.log4j.Level</code></a> class. 50 * <p> 51 * This adapter is capable to translate (I18N) messages logged with level >= 52 * <i>INFO</i>, while <i>DEBUG</i> messages are leaved as they are. 53 * <p> 54 * 55 * @author Ceki Gülcü 56 * @author <a href="mailto:acannone@imolinfo.it">Amedeo Cannone</a> 57 * @author <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a> 58 */ 59 final class Log4jLoggerAdapter extends MarkerIgnoringBase 60 implements LocationAwareLogger, it.imolinfo.jbi4cics.Logger { 61 62 /** 63 * Following the pattern discussed in pages 162 through 168 of "The 64 * complete log4j manual". 65 */ 66 private static final String FQCN = Log4jLoggerAdapter.class.getName(); 67 68 /** 69 * The Log4j logger adapted by this instance. 70 */ 71 private final Logger logger; 72 73 /** 74 * The optional <code>Messages</code> available to apply I18N to logged 75 * messages. 76 */ 77 private final Messages messages; 78 79 /** 80 * Creates a new adapter for the specificed Log4j logger. 81 * 82 * @param logger the Log4j logger. Must be not <code>null</code>. 83 * @param messages the optional <code>Messages</code> instance, 84 * responsible to apply I18N to messages logged by 85 * <code>logger</code>. It may be <code>null</code>, so 86 * there isn't I18N on logged messages. 87 */ 88 Log4jLoggerAdapter(final Logger logger, final Messages messages) { 89 this.logger = logger; 90 this.messages = messages; 91 } 92 93 /** 94 * Indicates if this <code>Logger</code> is applying internationalization 95 * to logged messages. 96 * 97 * @return <code>true</code> if and only if this instance is applying I18N 98 * to logged messages. 99 */ 100 private boolean isI18N() { 101 return messages != null; 102 } 103 104 /** 105 * Formats the specified message, applying I18N if it is available for this 106 * logger. 107 * 108 * @param format the format string. 109 * @param args the optional arguments. 110 * @return the formatted message, eventually internationalized. 111 */ 112 private String formatMessage(final String format, final Object ... args) { 113 String msg; 114 115 if (isI18N()) { 116 if (args.length == 0) { 117 msg = messages.getString(format); 118 } else { 119 msg = messages.getString(format, args); 120 } 121 } else { 122 if (args.length == 0) { 123 msg = format; 124 } else { 125 try { 126 msg = MessageFormat.format(format, args); 127 } catch (IllegalArgumentException e) { 128 msg = format; 129 } 130 } 131 } 132 return msg; 133 } 134 135 /** 136 * Gets the name of this <code>Logger</code>. 137 * 138 * @return the name of this <code>Logger</code> instance. 139 */ 140 public String getName() { 141 return logger.getName(); 142 } 143 144 /** 145 * Is this logger instance enabled for the DEBUG level? 146 * 147 * @return <code>true</code> if and only if this 148 * <code>org.slf4j.Logger</code> is enabled for level DEBUG. 149 */ 150 public boolean isDebugEnabled() { 151 return logger.isDebugEnabled(); 152 } 153 154 /** 155 * Log a message object at level DEBUG. 156 * 157 * @param msg the message string to be logged. 158 */ 159 public void debug(final String msg) { 160 logger.log(FQCN, Level.DEBUG, msg, null); 161 } 162 163 /** 164 * Log a message at level DEBUG according to the specified format and 165 * argument. 166 * <p> 167 * This form avoids superfluous object creation when the logger is disabled 168 * for level DEBUG. 169 * </p> 170 * 171 * @param format the format string. 172 * @param arg the argument. 173 */ 174 public void debug(final String format, final Object arg) { 175 if (isDebugEnabled()) { 176 177 // Debug level -> no I18N, argument formatting only 178 String msg = MessageFormat.format(format, arg); 179 180 logger.log(FQCN, Level.DEBUG, msg, null); 181 } 182 } 183 184 /** 185 * Log a message at level DEBUG according to the specified format and 186 * arguments. 187 * <p> 188 * This form avoids superfluous object creation when the logger is disabled 189 * for the DEBUG level. 190 * </p> 191 * 192 * @param format the format string. 193 * @param arg1 the first argument 194 * @param arg2 the second argument. 195 */ 196 public void debug( 197 final String format, final Object arg1, final Object arg2) { 198 if (isDebugEnabled()) { 199 200 // Debug level -> no I18N, argument formatting only 201 String msg = MessageFormat.format(format, arg1, arg2); 202 203 logger.log(FQCN, Level.DEBUG, msg, null); 204 } 205 } 206 207 /** 208 * Log a message at level DEBUG according to the specified format and 209 * arguments. 210 * <p> 211 * This form avoids superfluous object creation when the logger is disabled 212 * for the DEBUG level. 213 * </p> 214 * 215 * @param format the format string. 216 * @param args the arguments. 217 */ 218 public void debug(final String format, final Object[] args) { 219 if (isDebugEnabled()) { 220 221 // Debug level -> no I18N, argument formatting only 222 String msg = MessageFormat.format(format, args); 223 224 logger.log(FQCN, Level.DEBUG, msg, null); 225 } 226 } 227 228 /** 229 * Log an exception (throwable) at level DEBUG with an accompanying message. 230 * 231 * @param msg the message accompanying the exception. 232 * @param t the exception (throwable) to log. 233 */ 234 public void debug(final String msg, final Throwable t) { 235 236 // Debug level -> no I18N 237 logger.log(FQCN, Level.DEBUG, msg, t); 238 } 239 240 /** 241 * Is this logger instance enabled for the INFO level? 242 * 243 * @return <code>true</code> if and only if this 244 * <code>org.slf4j.Logger</code> is enabled for the INFO level. 245 */ 246 public boolean isInfoEnabled() { 247 return logger.isInfoEnabled(); 248 } 249 250 /** 251 * Log a message object at the INFO level. 252 * 253 * @param msg the message string to be logged. 254 */ 255 public void info(final String msg) { 256 if (isInfoEnabled()) { 257 logger.log(FQCN, Level.INFO, formatMessage(msg), null); 258 } 259 } 260 261 /** 262 * Log a message at level INFO according to the specified format and 263 * argument. 264 * <p> 265 * This form avoids superfluous object creation when the logger is disabled 266 * for the INFO level. 267 * </p> 268 * 269 * @param format the format string. 270 * @param arg the argument. 271 */ 272 public void info(final String format, final Object arg) { 273 if (isInfoEnabled()) { 274 logger.log(FQCN, Level.INFO, formatMessage(format, arg), null); 275 } 276 } 277 278 /** 279 * Log a message at the INFO level according to the specified format and 280 * arguments. 281 * <p> 282 * This form avoids superfluous object creation when the logger is disabled 283 * for the INFO level. 284 * </p> 285 * 286 * @param format the format string. 287 * @param arg1 the first argument. 288 * @param arg2 the second argument. 289 */ 290 public void info( 291 final String format, final Object arg1, final Object arg2) { 292 if (isInfoEnabled()) { 293 String msg = formatMessage(format, arg1, arg2); 294 295 logger.log(FQCN, Level.INFO, msg, null); 296 } 297 } 298 299 /** 300 * Log a message at level INFO according to the specified format and 301 * arguments. 302 * <p> 303 * This form avoids superfluous object creation when the logger is disabled 304 * for the INFO level. 305 * </p> 306 * 307 * @param format the format string. 308 * @param args the arguments. 309 */ 310 public void info(final String format, final Object[] args) { 311 if (isInfoEnabled()) { 312 logger.log(FQCN, Level.INFO, formatMessage(format, args), null); 313 } 314 } 315 316 /** 317 * Log an exception (throwable) at the INFO level with an accompanying 318 * message. 319 * 320 * @param msg the message accompanying the exception 321 * @param t the exception (throwable) to log. 322 */ 323 public void info(final String msg, final Throwable t) { 324 if (isInfoEnabled()) { 325 logger.log(FQCN, Level.INFO, formatMessage(msg), t); 326 } 327 } 328 329 /** 330 * Is this logger instance enabled for the WARN level? 331 * 332 * @return <code>true</code> if and only if this 333 * <code>org.slf4j.Logger</code> is enabled for the WARN level. 334 */ 335 public boolean isWarnEnabled() { 336 return logger.isEnabledFor(Level.WARN); 337 } 338 339 /** 340 * Log a message object at the WARN level. 341 * 342 * @param msg the message string to be logged. 343 */ 344 public void warn(final String msg) { 345 if (isWarnEnabled()) { 346 logger.log(FQCN, Level.WARN, formatMessage(msg), null); 347 } 348 } 349 350 /** 351 * Log a message at the WARN level according to the specified format and 352 * argument. 353 * <p> 354 * This form avoids superfluous object creation when the logger is disabled 355 * for the WARN level. 356 * </p> 357 * 358 * @param format the format string. 359 * @param arg the argument. 360 */ 361 public void warn(final String format, final Object arg) { 362 if (isWarnEnabled()) { 363 logger.log(FQCN, Level.WARN, formatMessage(format, arg), null); 364 } 365 } 366 367 /** 368 * Log a message at the WARN level according to the specified format and 369 * arguments. 370 * <p> 371 * This form avoids superfluous object creation when the logger is disabled 372 * for the WARN level. 373 * </p> 374 * 375 * @param format the format string. 376 * @param arg1 the first argument. 377 * @param arg2 the second argument. 378 */ 379 public void warn( 380 final String format, final Object arg1, final Object arg2) { 381 if (isWarnEnabled()) { 382 String msg = formatMessage(format, arg1, arg2); 383 384 logger.log(FQCN, Level.WARN, msg, null); 385 } 386 } 387 388 /** 389 * Log a message at level WARN according to the specified format and 390 * arguments. 391 * <p> 392 * This form avoids superfluous object creation when the logger is disabled 393 * for the WARN level. 394 * </p> 395 * 396 * @param format the format string. 397 * @param args the arguments. 398 */ 399 public void warn(final String format, final Object[] args) { 400 if (isWarnEnabled()) { 401 logger.log(FQCN, Level.WARN, formatMessage(format, args), null); 402 } 403 } 404 405 /** 406 * Log an exception (throwable) at the WARN level with an accompanying 407 * message. 408 * 409 * @param msg the message accompanying the exception. 410 * @param t the exception (throwable) to log. 411 */ 412 public void warn(final String msg, final Throwable t) { 413 if (isWarnEnabled()) { 414 logger.log(FQCN, Level.WARN, formatMessage(msg), t); 415 } 416 } 417 418 /** 419 * Is this logger instance enabled for level ERROR? 420 * 421 * @return <code>true</code> if and only if this 422 * <code>org.slf4j.Logger</code> is enabled for level ERROR. 423 */ 424 public boolean isErrorEnabled() { 425 return logger.isEnabledFor(Level.ERROR); 426 } 427 428 /** 429 * Log a message object at the ERROR level. 430 * 431 * @param msg the message string to be logged. 432 */ 433 public void error(final String msg) { 434 if (isErrorEnabled()) { 435 logger.log(FQCN, Level.ERROR, formatMessage(msg), null); 436 } 437 } 438 439 /** 440 * Log a message at the ERROR level according to the specified format 441 * and argument. 442 * <p> 443 * This form avoids superfluous object creation when the logger is disabled 444 * for the ERROR level. 445 * </p> 446 * 447 * @param format the format string. 448 * @param arg the argument. 449 */ 450 public void error(final String format, final Object arg) { 451 if (isErrorEnabled()) { 452 String msg = formatMessage(format, arg); 453 454 logger.log(FQCN, Level.ERROR, msg, null); 455 } 456 } 457 458 /** 459 * Log a message at the ERROR level according to the specified format and 460 * arguments. 461 * <p> 462 * This form avoids superfluous object creation when the logger is disabled 463 * for the ERROR level. 464 * </p> 465 * 466 * @param format the format string. 467 * @param arg1 the first argument. 468 * @param arg2 the second argument. 469 */ 470 public void error( 471 final String format, final Object arg1, final Object arg2) { 472 if (isErrorEnabled()) { 473 String msg = formatMessage(format, arg1, arg2); 474 475 logger.log(FQCN, Level.ERROR, msg, null); 476 } 477 } 478 479 /** 480 * Log a message at level ERROR according to the specified format and 481 * arguments. 482 * <p> 483 * This form avoids superfluous object creation when the logger is disabled 484 * for the ERROR level. 485 * </p> 486 * 487 * @param format the format string. 488 * @param args the arguments. 489 */ 490 public void error(final String format, final Object[] args) { 491 if (isErrorEnabled()) { 492 logger.log(FQCN, Level.ERROR, formatMessage(format, args), null); 493 } 494 } 495 496 /** 497 * Log an exception (throwable) at the ERROR level with an accompanying 498 * message. 499 * 500 * @param msg the message accompanying the exception. 501 * @param t the exception (throwable) to log. 502 */ 503 public void error(final String msg, final Throwable t) { 504 if (isErrorEnabled()) { 505 logger.log(FQCN, Level.ERROR, formatMessage(msg), t); 506 } 507 } 508 509 /** 510 * Printing method which support for location information. 511 * 512 * @param marker the marker. 513 * @param callerFQCN the fully qualified class name of the <b>caller</b>. 514 * @param level the level. 515 * @param msg the message. 516 * @param t the exception (throwable). 517 */ 518 public void log(final Marker marker, final String callerFQCN, 519 final int level, final String msg, final Throwable t) { 520 Level log4jLevel; 521 522 switch (level) { 523 case LocationAwareLogger.DEBUG_INT: 524 log4jLevel = Level.DEBUG; 525 break; 526 527 case LocationAwareLogger.INFO_INT: 528 log4jLevel = Level.INFO; 529 break; 530 531 case LocationAwareLogger.WARN_INT: 532 log4jLevel = Level.WARN; 533 break; 534 535 case LocationAwareLogger.ERROR_INT: 536 log4jLevel = Level.ERROR; 537 break; 538 539 default: 540 throw new IllegalArgumentException( 541 "Level number " + level + " is not recognized."); 542 } 543 logger.log(callerFQCN, log4jLevel, msg, t); 544 } 545 546 547 // New methods added to those provided by SLF4J: we want to log a formatted 548 // string and a Throwable 549 550 551 /** 552 * Log an exception (throwable) at level DEBUG with an accompanying message 553 * according to the specified format and arguments. 554 * <p> 555 * This form avoids superfluous object creation when the logger is disabled 556 * for the DEBUG level. 557 * </p> 558 * 559 * @param format the format string. 560 * @param args the arguments. 561 * @param t the exception (throwable) to log. 562 */ 563 public void debug( 564 final String format, final Object[] args, final Throwable t) { 565 if (isDebugEnabled()) { 566 567 // Debug level -> no I18N, argument formatting only 568 String msg = MessageFormat.format(format, args); 569 570 logger.log(FQCN, Level.DEBUG, msg, null); 571 } 572 } 573 574 /** 575 * Log an exception (throwable) at level INFO with an accompanying message 576 * according to the specified format and arguments. 577 * <p> 578 * This form avoids superfluous object creation when the logger is disabled 579 * for the INFO level. 580 * </p> 581 * 582 * @param format the format string. 583 * @param args the arguments. 584 * @param t the exception (throwable) to log. 585 */ 586 public void info( 587 final String format, final Object[] args, final Throwable t) { 588 if (isInfoEnabled()) { 589 logger.log(FQCN, Level.INFO, formatMessage(format, args), t); 590 } 591 } 592 593 /** 594 * Log an exception (throwable) at level WARN with an accompanying message 595 * according to the specified format and arguments. 596 * <p> 597 * This form avoids superfluous object creation when the logger is disabled 598 * for the WARN level. 599 * </p> 600 * 601 * @param format the format string. 602 * @param args the arguments. 603 * @param t the exception (throwable) to log. 604 */ 605 public void warn( 606 final String format, final Object[] args, final Throwable t) { 607 if (isWarnEnabled()) { 608 logger.log(FQCN, Level.WARN, formatMessage(format, args), t); 609 } 610 } 611 612 /** 613 * Log an exception (throwable) at level ERROR with an accompanying message 614 * according to the specified format and arguments. 615 * <p> 616 * This form avoids superfluous object creation when the logger is disabled 617 * for the ERROR level. 618 * </p> 619 * 620 * @param format the format string. 621 * @param args the arguments. 622 * @param t the exception (throwable) to log. 623 */ 624 public void error( 625 final String format, final Object[] args, final Throwable t) { 626 if (isErrorEnabled()) { 627 logger.log(FQCN, Level.ERROR, formatMessage(format, args), t); 628 } 629 } 630 }