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