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 org.apache.commons.logging.Log; 37 import org.slf4j.Logger; 38 import org.slf4j.helpers.MarkerIgnoringBase; 39 import org.slf4j.helpers.MessageFormatter; 40 41 /** 42 * A wrapper over {@link org.apache.commons.logging.Log 43 * org.apache.commons.logging.Log} in conformance with the {@link Logger} 44 * interface. 45 * 46 * @author Ceki Gülcü 47 */ 48 public final class JCLLoggerAdapter extends MarkerIgnoringBase { 49 final Log log; 50 final String name; 51 52 // WARN: JCLLoggerAdapter constructor should have only package access so 53 // that only JCLLoggerFactory be able to create one. 54 JCLLoggerAdapter(Log log, String name) { 55 this.log = log; 56 this.name = name; 57 } 58 59 public String getName() { 60 return name; 61 } 62 63 /** 64 * Delegates to the {@link Log#isTraceEnabled} method of the underlying 65 * {@link Log} instance. 66 */ 67 public boolean isTraceEnabled() { 68 return log.isTraceEnabled(); 69 } 70 71 // 72 73 /** 74 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 75 * {@link Log} instance. 76 * 77 * @param msg - the message object to be logged 78 */ 79 public void trace(String msg) { 80 log.trace(msg); 81 } 82 83 /** 84 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 85 * {@link Log} instance. 86 * 87 * <p> 88 * However, this form avoids superfluous object creation when the logger is disabled 89 * for level TRACE. 90 * </p> 91 * 92 * @param format 93 * the format string 94 * @param arg 95 * the argument 96 */ 97 public void trace(String format, Object arg) { 98 if (log.isDebugEnabled()) { 99 String msgStr = MessageFormatter.format(format, arg); 100 log.trace(msgStr); 101 } 102 } 103 104 /** 105 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 106 * {@link Log} instance. 107 * 108 * <p> 109 * However, this form avoids superfluous object creation when the logger is disabled 110 * for level TRACE. 111 * </p> 112 * 113 * @param format 114 * the format string 115 * @param arg1 116 * the first argument 117 * @param arg2 118 * the second argument 119 */ 120 public void trace(String format, Object arg1, Object arg2) { 121 if (log.isDebugEnabled()) { 122 String msgStr = MessageFormatter.format(format, arg1, arg2); 123 log.trace(msgStr); 124 } 125 } 126 127 128 /** 129 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 130 * {@link Log} instance. 131 * 132 * <p> 133 * However, this form avoids superfluous object creation when the logger is disabled 134 * for level TRACE. 135 * </p> 136 * 137 * @param format the format string 138 * @param argArray an array of arguments 139 */ 140 public void trace(String format, Object[] argArray) { 141 if (log.isDebugEnabled()) { 142 String msgStr = MessageFormatter.arrayFormat(format, argArray); 143 log.trace(msgStr); 144 } 145 } 146 147 /** 148 * Delegates to the {@link Log#trace(java.lang.Object, java.lang.Throwable)} method of 149 * the underlying {@link Log} instance. 150 * 151 * @param msg 152 * the message accompanying the exception 153 * @param t 154 * the exception (throwable) to log 155 */ 156 public void trace(String msg, Throwable t) { 157 log.trace(msg, t); 158 } 159 160 161 /** 162 * Delegates to the {@link Log#isDebugEnabled} method of the underlying 163 * {@link Log} instance. 164 */ 165 public boolean isDebugEnabled() { 166 return log.isDebugEnabled(); 167 } 168 169 // 170 171 /** 172 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 173 * {@link Log} instance. 174 * 175 * @param msg - the message object to be logged 176 */ 177 public void debug(String msg) { 178 log.debug(msg); 179 } 180 181 /** 182 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 183 * {@link Log} instance. 184 * 185 * <p> 186 * However, this form avoids superfluous object creation when the logger is disabled 187 * for level DEBUG. 188 * </p> 189 * 190 * @param format 191 * the format string 192 * @param arg 193 * the argument 194 */ 195 public void debug(String format, Object arg) { 196 if (log.isDebugEnabled()) { 197 String msgStr = MessageFormatter.format(format, arg); 198 log.debug(msgStr); 199 } 200 } 201 202 /** 203 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 204 * {@link Log} instance. 205 * 206 * <p> 207 * However, this form avoids superfluous object creation when the logger is disabled 208 * for level DEBUG. 209 * </p> 210 * 211 * @param format 212 * the format string 213 * @param arg1 214 * the first argument 215 * @param arg2 216 * the second argument 217 */ 218 public void debug(String format, Object arg1, Object arg2) { 219 if (log.isDebugEnabled()) { 220 String msgStr = MessageFormatter.format(format, arg1, arg2); 221 log.debug(msgStr); 222 } 223 } 224 225 226 /** 227 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 228 * {@link Log} instance. 229 * 230 * <p> 231 * However, this form avoids superfluous object creation when the logger is disabled 232 * for level DEBUG. 233 * </p> 234 * 235 * @param format the format string 236 * @param argArray an array of arguments 237 */ 238 public void debug(String format, Object[] argArray) { 239 if (log.isDebugEnabled()) { 240 String msgStr = MessageFormatter.arrayFormat(format, argArray); 241 log.debug(msgStr); 242 } 243 } 244 245 /** 246 * Delegates to the {@link Log#debug(java.lang.Object, java.lang.Throwable)} method of 247 * the underlying {@link Log} instance. 248 * 249 * @param msg 250 * the message accompanying the exception 251 * @param t 252 * the exception (throwable) to log 253 */ 254 public void debug(String msg, Throwable t) { 255 log.debug(msg, t); 256 } 257 258 /** 259 * Delegates to the {@link Log#isInfoEnabled} method of the underlying 260 * {@link Log} instance. 261 */ 262 public boolean isInfoEnabled() { 263 return log.isInfoEnabled(); 264 } 265 266 /** 267 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 268 * {@link Log} instance. 269 * 270 * @param msg - the message object to be logged 271 */ 272 public void info(String msg) { 273 log.info(msg); 274 } 275 276 /** 277 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 278 * {@link Log} instance. 279 * 280 * <p> 281 * However, this form avoids superfluous object creation when the logger is disabled 282 * for level INFO. 283 * </p> 284 * 285 * @param format 286 * the format string 287 * @param arg 288 * the argument 289 */ 290 291 public void info(String format, Object arg) { 292 if (log.isInfoEnabled()) { 293 String msgStr = MessageFormatter.format(format, arg); 294 log.info(msgStr); 295 } 296 } 297 /** 298 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 299 * {@link Log} instance. 300 * 301 * <p> 302 * However, this form avoids superfluous object creation when the logger is disabled 303 * for level INFO. 304 * </p> 305 * 306 * @param format 307 * the format string 308 * @param arg1 309 * the first argument 310 * @param arg2 311 * the second argument 312 */ 313 public void info(String format, Object arg1, Object arg2) { 314 if (log.isInfoEnabled()) { 315 String msgStr = MessageFormatter.format(format, arg1, arg2); 316 log.info(msgStr); 317 } 318 } 319 320 /** 321 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 322 * {@link Log} instance. 323 * 324 * <p> 325 * However, this form avoids superfluous object creation when the logger is disabled 326 * for level INFO. 327 * </p> 328 * 329 * @param format the format string 330 * @param argArray an array of arguments 331 */ 332 public void info(String format, Object[] argArray) { 333 if (log.isInfoEnabled()) { 334 String msgStr = MessageFormatter.arrayFormat(format, argArray); 335 log.info(msgStr); 336 } 337 } 338 339 340 /** 341 * Delegates to the {@link Log#info(java.lang.Object, java.lang.Throwable)} method of 342 * the underlying {@link Log} instance. 343 * 344 * @param msg 345 * the message accompanying the exception 346 * @param t 347 * the exception (throwable) to log 348 */ 349 public void info(String msg, Throwable t) { 350 log.info(msg, t); 351 } 352 353 /** 354 * Delegates to the {@link Log#isWarnEnabled} method of the underlying 355 * {@link Log} instance. 356 */ 357 public boolean isWarnEnabled() { 358 return log.isWarnEnabled(); 359 } 360 361 /** 362 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 363 * {@link Log} instance. 364 * 365 * @param msg - the message object to be logged 366 */ 367 public void warn(String msg) { 368 log.warn(msg); 369 } 370 371 /** 372 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 373 * {@link Log} instance. 374 * 375 * <p> 376 * However, this form avoids superfluous object creation when the logger is disabled 377 * for level WARN. 378 * </p> 379 * 380 * @param format 381 * the format string 382 * @param arg 383 * the argument 384 */ 385 public void warn(String format, Object arg) { 386 if (log.isWarnEnabled()) { 387 String msgStr = MessageFormatter.format(format, arg); 388 log.warn(msgStr); 389 } 390 } 391 392 /** 393 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 394 * {@link Log} instance. 395 * 396 * <p> 397 * However, this form avoids superfluous object creation when the logger is disabled 398 * for level WARN. 399 * </p> 400 * 401 * @param format 402 * the format string 403 * @param arg1 404 * the first argument 405 * @param arg2 406 * the second argument 407 */ 408 public void warn(String format, Object arg1, Object arg2) { 409 if (log.isWarnEnabled()) { 410 String msgStr = MessageFormatter.format(format, arg1, arg2); 411 log.warn(msgStr); 412 } 413 } 414 415 /** 416 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 417 * {@link Log} instance. 418 * 419 * <p> 420 * However, this form avoids superfluous object creation when the logger is disabled 421 * for level WARN. 422 * </p> 423 * 424 * @param format the format string 425 * @param argArray an array of arguments 426 */ 427 public void warn(String format, Object[] argArray) { 428 if (log.isWarnEnabled()) { 429 String msgStr = MessageFormatter.arrayFormat(format, argArray); 430 log.warn(msgStr); 431 } 432 } 433 434 435 /** 436 * Delegates to the {@link Log#warn(java.lang.Object, java.lang.Throwable)} method of 437 * the underlying {@link Log} instance. 438 * 439 * @param msg 440 * the message accompanying the exception 441 * @param t 442 * the exception (throwable) to log 443 */ 444 445 public void warn(String msg, Throwable t) { 446 log.warn(msg, t); 447 } 448 449 450 /** 451 * Delegates to the {@link Log#isErrorEnabled} method of the underlying 452 * {@link Log} instance. 453 */ 454 public boolean isErrorEnabled() { 455 return log.isErrorEnabled(); 456 } 457 458 /** 459 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 460 * {@link Log} instance. 461 * 462 * @param msg - the message object to be logged 463 */ 464 public void error(String msg) { 465 log.error(msg); 466 } 467 468 /** 469 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 470 * {@link Log} instance. 471 * 472 * <p> 473 * However, this form avoids superfluous object creation when the logger is disabled 474 * for level ERROR. 475 * </p> 476 * 477 * @param format 478 * the format string 479 * @param arg 480 * the argument 481 */ 482 public void error(String format, Object arg) { 483 if (log.isErrorEnabled()) { 484 String msgStr = MessageFormatter.format(format, arg); 485 log.error(msgStr); 486 } 487 } 488 489 /** 490 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 491 * {@link Log} instance. 492 * 493 * <p> 494 * However, this form avoids superfluous object creation when the logger is disabled 495 * for level ERROR. 496 * </p> 497 * 498 * @param format 499 * the format string 500 * @param arg1 501 * the first argument 502 * @param arg2 503 * the second argument 504 */ 505 public void error(String format, Object arg1, Object arg2) { 506 if (log.isErrorEnabled()) { 507 String msgStr = MessageFormatter.format(format, arg1, arg2); 508 log.error(msgStr); 509 } 510 } 511 512 /** 513 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 514 * {@link Log} instance. 515 * 516 * <p> 517 * However, this form avoids superfluous object creation when the logger is disabled 518 * for level ERROR. 519 * </p> 520 * 521 * @param format the format string 522 * @param argArray an array of arguments 523 */ 524 public void error(String format, Object[] argArray) { 525 if (log.isErrorEnabled()) { 526 String msgStr = MessageFormatter.arrayFormat(format, argArray); 527 log.error(msgStr); 528 } 529 } 530 531 532 /** 533 * Delegates to the {@link Log#error(java.lang.Object, java.lang.Throwable)} method of 534 * the underlying {@link Log} instance. 535 * 536 * @param msg 537 * the message accompanying the exception 538 * @param t 539 * the exception (throwable) to log 540 */ 541 542 public void error(String msg, Throwable t) { 543 log.error(msg, t); 544 } 545 546 }