1 25 26 package org.objectweb.easybeans.log; 27 28 import java.text.MessageFormat ; 29 30 import org.apache.commons.logging.Log; 31 32 import org.objectweb.easybeans.i18n.I18n; 33 34 38 public class CommonsLoggerImpl implements JLog { 39 40 43 private static final long serialVersionUID = 1707614695786252792L; 44 45 48 private Log wrapped; 49 50 53 private I18n i18n = null; 54 55 60 public CommonsLoggerImpl(final Log wrapped, final I18n i18n) { 61 this.wrapped = wrapped; 62 this.i18n = i18n; 63 } 64 65 76 public boolean isDebugEnabled() { 77 return wrapped.isDebugEnabled(); 78 } 79 80 91 public boolean isErrorEnabled() { 92 return wrapped.isErrorEnabled(); 93 } 94 95 106 public boolean isFatalEnabled() { 107 return wrapped.isFatalEnabled(); 108 } 109 110 121 public boolean isInfoEnabled() { 122 return wrapped.isInfoEnabled(); 123 } 124 125 136 public boolean isTraceEnabled() { 137 return wrapped.isDebugEnabled(); 138 } 139 140 151 public boolean isWarnEnabled() { 152 return wrapped.isWarnEnabled(); 153 } 154 155 161 public void trace(final Object message) { 162 wrapped.trace(message); 163 } 164 165 179 public void trace(final Object message, final Object ... args) { 180 if (!wrapped.isTraceEnabled()) { 182 return; 183 } 184 String msg = getMessage(message, args); 185 Throwable t = getThrowable(args); 186 if (t != null) { 187 wrapped.trace(msg, t); 188 } else { 189 wrapped.trace(msg); 190 } 191 } 192 193 207 public void debug(final Object message, final Object ... args) { 208 if (!wrapped.isDebugEnabled()) { 210 return; 211 } 212 String msg = getMessage(message, args); 213 Throwable t = getThrowable(args); 214 if (t != null) { 215 wrapped.debug(msg, t); 216 } else { 217 wrapped.debug(msg); 218 } 219 } 220 221 235 public void info(final Object message, final Object ... args) { 236 if (!wrapped.isInfoEnabled()) { 238 return; 239 } 240 241 String msg = getMessage(message, args); 242 Throwable t = getThrowable(args); 243 if (t != null) { 244 wrapped.info(msg, t); 245 } else { 246 wrapped.info(msg); 247 } 248 249 } 250 251 265 public void warn(final Object message, final Object ... args) { 266 if (!wrapped.isWarnEnabled()) { 268 return; 269 } 270 271 String msg = getMessage(message, args); 272 Throwable t = getThrowable(args); 273 if (t != null) { 274 wrapped.warn(msg, t); 275 } else { 276 wrapped.warn(msg); 277 } 278 279 } 280 281 295 public void error(final Object message, final Object ... args) { 296 if (!wrapped.isErrorEnabled()) { 298 return; 299 } 300 301 String msg = getMessage(message, args); 302 Throwable t = getThrowable(args); 303 if (t != null) { 304 wrapped.error(msg, t); 305 } else { 306 wrapped.error(msg); 307 } 308 309 } 310 311 325 public void fatal(final Object message, final Object ... args) { 326 if (!wrapped.isFatalEnabled()) { 328 return; 329 } 330 331 String msg = getMessage(message, args); 332 Throwable t = getThrowable(args); 333 if (t != null) { 334 wrapped.fatal(msg, t); 335 } else { 336 wrapped.fatal(msg); 337 } 338 339 } 340 341 345 public I18n getI18n() { 346 return i18n; 347 } 348 349 350 358 private String getMessage(final Object message, final Object ... args) { 359 360 if (i18n != null) { 362 return i18n.getMessage(String.valueOf(message), args); 364 } 365 366 return MessageFormat.format(String.valueOf(message), args); 368 } 369 370 376 private Throwable getThrowable(final Object ... args) { 377 int size = args.length; 378 if (size > 0) { 380 Object lastArg = args[size - 1]; 381 if (lastArg instanceof Throwable ) { 382 return (Throwable ) lastArg; 383 } 384 } 385 386 return null; 388 } 389 390 } 391 | Popular Tags |