JCLLoggerAdapter.java revision 3c0ab3466b6fa6e915974c72558d64c570734700
1/* 2 * Copyright (c) 2004-2008 QOS.ch 3 * All rights reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 26package org.slf4j.impl; 27 28import org.apache.commons.logging.Log; 29import org.slf4j.Logger; 30import org.slf4j.helpers.FormattingTuple; 31import org.slf4j.helpers.MarkerIgnoringBase; 32import org.slf4j.helpers.MessageFormatter; 33 34/** 35 * A wrapper over {@link org.apache.commons.logging.Log 36 * org.apache.commons.logging.Log} in conformance with the {@link Logger} 37 * interface. 38 * 39 * @author Ceki Gülcü 40 */ 41public final class JCLLoggerAdapter extends MarkerIgnoringBase { 42 43 private static final long serialVersionUID = 4141593417490482209L; 44 final Log log; 45 46 // WARN: JCLLoggerAdapter constructor should have only package access so 47 // that only JCLLoggerFactory be able to create one. 48 JCLLoggerAdapter(Log log, String name) { 49 this.log = log; 50 this.name = name; 51 } 52 53 /** 54 * Delegates to the {@link Log#isTraceEnabled} method of the underlying 55 * {@link Log} instance. 56 */ 57 public boolean isTraceEnabled() { 58 return log.isTraceEnabled(); 59 } 60 61 // 62 63 /** 64 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 65 * {@link Log} instance. 66 * 67 * @param msg - the message object to be logged 68 */ 69 public void trace(String msg) { 70 log.trace(msg); 71 } 72 73 /** 74 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 75 * {@link Log} instance. 76 * 77 * <p> 78 * However, this form avoids superfluous object creation when the logger is disabled 79 * for level TRACE. 80 * </p> 81 * 82 * @param format 83 * the format string 84 * @param arg 85 * the argument 86 */ 87 public void trace(String format, Object arg) { 88 if (log.isDebugEnabled()) { 89 FormattingTuple ft = MessageFormatter.format(format, arg); 90 log.trace(ft.getMessage(), ft.getThrowable()); 91 } 92 } 93 94 /** 95 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 96 * {@link Log} instance. 97 * 98 * <p> 99 * However, this form avoids superfluous object creation when the logger is disabled 100 * for level TRACE. 101 * </p> 102 * 103 * @param format 104 * the format string 105 * @param arg1 106 * the first argument 107 * @param arg2 108 * the second argument 109 */ 110 public void trace(String format, Object arg1, Object arg2) { 111 if (log.isDebugEnabled()) { 112 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); 113 log.trace(ft.getMessage(), ft.getThrowable()); 114 } 115 } 116 117 118 /** 119 * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying 120 * {@link Log} instance. 121 * 122 * <p> 123 * However, this form avoids superfluous object creation when the logger is disabled 124 * for level TRACE. 125 * </p> 126 * 127 * @param format the format string 128 * @param argArray an array of arguments 129 */ 130 public void trace(String format, Object[] argArray) { 131 if (log.isDebugEnabled()) { 132 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); 133 log.trace(ft.getMessage(), ft.getThrowable()); 134 } 135 } 136 137 /** 138 * Delegates to the {@link Log#trace(java.lang.Object, java.lang.Throwable)} method of 139 * the underlying {@link Log} instance. 140 * 141 * @param msg 142 * the message accompanying the exception 143 * @param t 144 * the exception (throwable) to log 145 */ 146 public void trace(String msg, Throwable t) { 147 log.trace(msg, t); 148 } 149 150 151 /** 152 * Delegates to the {@link Log#isDebugEnabled} method of the underlying 153 * {@link Log} instance. 154 */ 155 public boolean isDebugEnabled() { 156 return log.isDebugEnabled(); 157 } 158 159 // 160 161 /** 162 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 163 * {@link Log} instance. 164 * 165 * @param msg - the message object to be logged 166 */ 167 public void debug(String msg) { 168 log.debug(msg); 169 } 170 171 /** 172 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 173 * {@link Log} instance. 174 * 175 * <p> 176 * However, this form avoids superfluous object creation when the logger is disabled 177 * for level DEBUG. 178 * </p> 179 * 180 * @param format 181 * the format string 182 * @param arg 183 * the argument 184 */ 185 public void debug(String format, Object arg) { 186 if (log.isDebugEnabled()) { 187 FormattingTuple ft = MessageFormatter.format(format, arg); 188 log.debug(ft.getMessage(), ft.getThrowable()); 189 } 190 } 191 192 /** 193 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 194 * {@link Log} instance. 195 * 196 * <p> 197 * However, this form avoids superfluous object creation when the logger is disabled 198 * for level DEBUG. 199 * </p> 200 * 201 * @param format 202 * the format string 203 * @param arg1 204 * the first argument 205 * @param arg2 206 * the second argument 207 */ 208 public void debug(String format, Object arg1, Object arg2) { 209 if (log.isDebugEnabled()) { 210 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); 211 log.debug(ft.getMessage(), ft.getThrowable()); 212 } 213 } 214 215 216 /** 217 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 218 * {@link Log} instance. 219 * 220 * <p> 221 * However, this form avoids superfluous object creation when the logger is disabled 222 * for level DEBUG. 223 * </p> 224 * 225 * @param format the format string 226 * @param argArray an array of arguments 227 */ 228 public void debug(String format, Object[] argArray) { 229 if (log.isDebugEnabled()) { 230 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); 231 log.debug(ft.getMessage(), ft.getThrowable()); 232 } 233 } 234 235 /** 236 * Delegates to the {@link Log#debug(java.lang.Object, java.lang.Throwable)} method of 237 * the underlying {@link Log} instance. 238 * 239 * @param msg 240 * the message accompanying the exception 241 * @param t 242 * the exception (throwable) to log 243 */ 244 public void debug(String msg, Throwable t) { 245 log.debug(msg, t); 246 } 247 248 /** 249 * Delegates to the {@link Log#isInfoEnabled} method of the underlying 250 * {@link Log} instance. 251 */ 252 public boolean isInfoEnabled() { 253 return log.isInfoEnabled(); 254 } 255 256 /** 257 * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying 258 * {@link Log} instance. 259 * 260 * @param msg - the message object to be logged 261 */ 262 public void info(String msg) { 263 log.info(msg); 264 } 265 266 /** 267 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 268 * {@link Log} instance. 269 * 270 * <p> 271 * However, this form avoids superfluous object creation when the logger is disabled 272 * for level INFO. 273 * </p> 274 * 275 * @param format 276 * the format string 277 * @param arg 278 * the argument 279 */ 280 281 public void info(String format, Object arg) { 282 if (log.isInfoEnabled()) { 283 FormattingTuple ft = MessageFormatter.format(format, arg); 284 log.info(ft.getMessage(), ft.getThrowable()); 285 } 286 } 287 /** 288 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 289 * {@link Log} instance. 290 * 291 * <p> 292 * However, this form avoids superfluous object creation when the logger is disabled 293 * for level INFO. 294 * </p> 295 * 296 * @param format 297 * the format string 298 * @param arg1 299 * the first argument 300 * @param arg2 301 * the second argument 302 */ 303 public void info(String format, Object arg1, Object arg2) { 304 if (log.isInfoEnabled()) { 305 306 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); 307 log.info(ft.getMessage(), ft.getThrowable()); 308 } 309 } 310 311 /** 312 * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying 313 * {@link Log} instance. 314 * 315 * <p> 316 * However, this form avoids superfluous object creation when the logger is disabled 317 * for level INFO. 318 * </p> 319 * 320 * @param format the format string 321 * @param argArray an array of arguments 322 */ 323 public void info(String format, Object[] argArray) { 324 if (log.isInfoEnabled()) { 325 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); 326 log.info(ft.getMessage(), ft.getThrowable()); 327 } 328 } 329 330 331 /** 332 * Delegates to the {@link Log#info(java.lang.Object, java.lang.Throwable)} method of 333 * the underlying {@link Log} instance. 334 * 335 * @param msg 336 * the message accompanying the exception 337 * @param t 338 * the exception (throwable) to log 339 */ 340 public void info(String msg, Throwable t) { 341 log.info(msg, t); 342 } 343 344 /** 345 * Delegates to the {@link Log#isWarnEnabled} method of the underlying 346 * {@link Log} instance. 347 */ 348 public boolean isWarnEnabled() { 349 return log.isWarnEnabled(); 350 } 351 352 /** 353 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 354 * {@link Log} instance. 355 * 356 * @param msg - the message object to be logged 357 */ 358 public void warn(String msg) { 359 log.warn(msg); 360 } 361 362 /** 363 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 364 * {@link Log} instance. 365 * 366 * <p> 367 * However, this form avoids superfluous object creation when the logger is disabled 368 * for level WARN. 369 * </p> 370 * 371 * @param format 372 * the format string 373 * @param arg 374 * the argument 375 */ 376 public void warn(String format, Object arg) { 377 if (log.isWarnEnabled()) { 378 FormattingTuple ft = MessageFormatter.format(format, arg); 379 log.warn(ft.getMessage(), ft.getThrowable()); 380 } 381 } 382 383 /** 384 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 385 * {@link Log} instance. 386 * 387 * <p> 388 * However, this form avoids superfluous object creation when the logger is disabled 389 * for level WARN. 390 * </p> 391 * 392 * @param format 393 * the format string 394 * @param arg1 395 * the first argument 396 * @param arg2 397 * the second argument 398 */ 399 public void warn(String format, Object arg1, Object arg2) { 400 if (log.isWarnEnabled()) { 401 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); 402 log.warn(ft.getMessage(), ft.getThrowable()); 403 } 404 } 405 406 /** 407 * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying 408 * {@link Log} instance. 409 * 410 * <p> 411 * However, this form avoids superfluous object creation when the logger is disabled 412 * for level WARN. 413 * </p> 414 * 415 * @param format the format string 416 * @param argArray an array of arguments 417 */ 418 public void warn(String format, Object[] argArray) { 419 if (log.isWarnEnabled()) { 420 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); 421 log.warn(ft.getMessage(), ft.getThrowable()); 422 } 423 } 424 425 426 /** 427 * Delegates to the {@link Log#warn(java.lang.Object, java.lang.Throwable)} method of 428 * the underlying {@link Log} instance. 429 * 430 * @param msg 431 * the message accompanying the exception 432 * @param t 433 * the exception (throwable) to log 434 */ 435 436 public void warn(String msg, Throwable t) { 437 log.warn(msg, t); 438 } 439 440 441 /** 442 * Delegates to the {@link Log#isErrorEnabled} method of the underlying 443 * {@link Log} instance. 444 */ 445 public boolean isErrorEnabled() { 446 return log.isErrorEnabled(); 447 } 448 449 /** 450 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 451 * {@link Log} instance. 452 * 453 * @param msg - the message object to be logged 454 */ 455 public void error(String msg) { 456 log.error(msg); 457 } 458 459 /** 460 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 461 * {@link Log} instance. 462 * 463 * <p> 464 * However, this form avoids superfluous object creation when the logger is disabled 465 * for level ERROR. 466 * </p> 467 * 468 * @param format 469 * the format string 470 * @param arg 471 * the argument 472 */ 473 public void error(String format, Object arg) { 474 if (log.isErrorEnabled()) { 475 FormattingTuple ft = MessageFormatter.format(format, arg); 476 log.error(ft.getMessage(), ft.getThrowable()); 477 } 478 } 479 480 /** 481 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 482 * {@link Log} instance. 483 * 484 * <p> 485 * However, this form avoids superfluous object creation when the logger is disabled 486 * for level ERROR. 487 * </p> 488 * 489 * @param format 490 * the format string 491 * @param arg1 492 * the first argument 493 * @param arg2 494 * the second argument 495 */ 496 public void error(String format, Object arg1, Object arg2) { 497 if (log.isErrorEnabled()) { 498 FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); 499 log.error(ft.getMessage(), ft.getThrowable()); 500 } 501 } 502 503 /** 504 * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying 505 * {@link Log} instance. 506 * 507 * <p> 508 * However, this form avoids superfluous object creation when the logger is disabled 509 * for level ERROR. 510 * </p> 511 * 512 * @param format the format string 513 * @param argArray an array of arguments 514 */ 515 public void error(String format, Object[] argArray) { 516 if (log.isErrorEnabled()) { 517 FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); 518 log.error(ft.getMessage(), ft.getThrowable()); 519 } 520 } 521 522 523 /** 524 * Delegates to the {@link Log#error(java.lang.Object, java.lang.Throwable)} method of 525 * the underlying {@link Log} instance. 526 * 527 * @param msg 528 * the message accompanying the exception 529 * @param t 530 * the exception (throwable) to log 531 */ 532 533 public void error(String msg, Throwable t) { 534 log.error(msg, t); 535 } 536 537} 538