1 25 26 package org.objectweb.easybeans.log; 27 28 import java.io.Serializable ; 29 import java.text.MessageFormat ; 30 import java.util.logging.ConsoleHandler ; 31 import java.util.logging.Handler ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 35 import org.objectweb.easybeans.i18n.I18n; 36 37 41 public class JDKLogger implements JLog, Serializable { 42 43 46 private static final long serialVersionUID = -3786299204538237422L; 47 48 51 private String name = null; 52 53 56 private transient Logger logger = null; 57 58 61 private I18n i18n = null; 62 63 66 private static boolean handlerSet = false; 67 68 71 public static final String SET_HANDLER = "easybeans.log.handler"; 72 73 78 public JDKLogger(final Class clazz, final I18n i18n) { 79 this.name = clazz.getName(); 80 this.i18n = i18n; 81 logger = getLogger(); 82 83 } 84 85 91 private void log(final Level level, final String msg, final Throwable t) { 92 Throwable throwable = new Throwable (); 94 StackTraceElement [] stackTraceElements = throwable.getStackTrace(); 95 96 String className = "stack-not-found"; 98 String methodName = "stack-not-found"; 99 if (stackTraceElements != null && stackTraceElements.length > 2) { 100 StackTraceElement caller = stackTraceElements[2]; 101 className = caller.getClassName(); 102 methodName = caller.getMethodName(); 103 } 104 105 Logger logger = getLogger(); 107 108 if (t == null) { 110 logger.logp(level, className, methodName, msg); 111 } else { 112 logger.logp(level, className, methodName, msg, t); 113 } 114 115 } 116 117 121 private void replaceConsoleHandlerInParentHandlers(final Logger logger) { 122 if (logger == null) { 123 return; 124 } 125 Logger parent = logger.getParent(); 126 if (parent == null) { 127 return; 128 } 129 Handler [] handlers = parent.getHandlers(); 130 if (handlers != null) { 131 for (Handler handler : handlers) { 132 if (handler instanceof ConsoleHandler ) { 133 parent.removeHandler(handler); 134 Handler easyBeansHandler = new JDKConsoleHandler(); 136 easyBeansHandler.setFormatter(new JDKFormatter()); 137 parent.addHandler(easyBeansHandler); 138 } 139 } 140 } 141 } 142 143 146 public Logger getLogger() { 147 if (logger == null) { 148 logger = Logger.getLogger(name); 149 } 150 151 152 if (!handlerSet) { 154 String prop = System.getProperty(SET_HANDLER, "true"); 155 if (Boolean.parseBoolean(prop)) { 157 replaceConsoleHandlerInParentHandlers(logger); 158 159 } 160 handlerSet = true; 162 } 163 return logger; 164 } 165 166 177 public boolean isDebugEnabled() { 178 return (getLogger().isLoggable(Level.FINE)); 179 } 180 181 192 public boolean isErrorEnabled() { 193 return (getLogger().isLoggable(Level.SEVERE)); 194 } 195 196 207 public boolean isFatalEnabled() { 208 return (getLogger().isLoggable(Level.SEVERE)); 209 } 210 211 222 public boolean isInfoEnabled() { 223 return (getLogger().isLoggable(Level.INFO)); 224 } 225 226 237 public boolean isTraceEnabled() { 238 return (getLogger().isLoggable(Level.FINEST)); 239 } 240 241 252 public boolean isWarnEnabled() { 253 return (getLogger().isLoggable(Level.WARNING)); 254 } 255 256 260 public I18n getI18n() { 261 return i18n; 262 } 263 264 278 public void debug(final Object message, final Object ... args) { 279 if (!isDebugEnabled()) { 281 return; 282 } 283 284 String msg = getMessage(message, args); 285 Throwable t = getThrowable(args); 286 log(Level.FINE, msg, t); 287 288 } 289 290 304 public void error(final Object message, final Object ... args) { 305 String msg = getMessage(message, args); 306 Throwable t = getThrowable(args); 307 log(Level.SEVERE, msg, t); 308 } 309 310 324 public void fatal(final Object message, final Object ... args) { 325 error(message, args); 327 } 328 329 343 public void info(final Object message, final Object ... args) { 344 if (!isInfoEnabled()) { 346 return; 347 } 348 349 String msg = getMessage(message, args); 350 Throwable t = getThrowable(args); 351 log(Level.INFO, msg, t); 352 } 353 354 368 public void trace(final Object message, final Object ... args) { 369 if (!isTraceEnabled()) { 371 return; 372 } 373 374 String msg = getMessage(message, args); 375 Throwable t = getThrowable(args); 376 log(Level.FINEST, msg, t); 377 378 } 379 380 394 public void warn(final Object message, final Object ... args) { 395 if (!isWarnEnabled()) { 397 return; 398 } 399 400 String msg = getMessage(message, args); 401 Throwable t = getThrowable(args); 402 log(Level.WARNING, msg, t); 403 404 } 405 406 414 private String getMessage(final Object message, final Object ... args) { 415 416 if (i18n != null) { 418 return i18n.getMessage(String.valueOf(message), args); 420 } 421 422 return MessageFormat.format(String.valueOf(message), args); 424 } 425 426 432 private Throwable getThrowable(final Object ... args) { 433 int size = args.length; 434 if (size > 0) { 436 Object lastArg = args[size - 1]; 437 if (lastArg instanceof Throwable ) { 438 return (Throwable ) lastArg; 439 } 440 } 441 442 return null; 444 } 445 446 } 447 | Popular Tags |