1 22 package org.jboss.logging; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.Serializable ; 27 28 60 public class Logger implements Serializable 61 { 62 63 private static final long serialVersionUID = 4232175575988879434L; 64 65 66 protected static String PLUGIN_CLASS_PROP = "org.jboss.logging.Logger.pluginClass"; 67 68 69 protected static final String LOG4J_PLUGIN_CLASS_NAME = "org.jboss.logging.log4j.Log4jLoggerPlugin"; 70 71 72 protected static Class pluginClass = null; 73 74 75 protected static String pluginClassName = null; 76 77 static 78 { 79 init(); 80 } 81 82 83 private final String name; 84 85 86 protected transient LoggerPlugin loggerDelegate = null; 87 88 92 public static String getPluginClassName() 93 { 94 return Logger.pluginClassName; 95 } 96 97 102 public static void setPluginClassName(String pluginClassName) 103 { 104 if (pluginClassName.equals(Logger.pluginClassName) == false) 105 { 106 Logger.pluginClassName = pluginClassName; 107 init(); 108 } 109 } 110 111 116 protected Logger(final String name) 117 { 118 this.name = name; 119 this.loggerDelegate = getDelegatePlugin(name); 120 } 121 122 127 public String getName() 128 { 129 return name; 130 } 131 132 137 public LoggerPlugin getLoggerPlugin() 138 { 139 return this.loggerDelegate; 140 } 141 142 148 public boolean isTraceEnabled() 149 { 150 return loggerDelegate.isTraceEnabled(); 151 } 152 153 158 public void trace(Object message) 159 { 160 loggerDelegate.trace(message); 161 } 162 163 169 public void trace(Object message, Throwable t) 170 { 171 loggerDelegate.trace(message, t); 172 } 173 174 181 public boolean isDebugEnabled() 182 { 183 return loggerDelegate.isDebugEnabled(); 184 } 185 186 191 public void debug(Object message) 192 { 193 loggerDelegate.debug(message); 194 } 195 196 202 public void debug(Object message, Throwable t) 203 { 204 loggerDelegate.debug(message, t); 205 } 206 207 214 public boolean isInfoEnabled() 215 { 216 return loggerDelegate.isInfoEnabled(); 217 } 218 219 224 public void info(Object message) 225 { 226 loggerDelegate.info(message); 227 } 228 229 235 public void info(Object message, Throwable t) 236 { 237 loggerDelegate.info(message, t); 238 } 239 240 245 public void warn(Object message) 246 { 247 loggerDelegate.warn(message); 248 } 249 250 256 public void warn(Object message, Throwable t) 257 { 258 loggerDelegate.warn(message, t); 259 } 260 261 266 public void error(Object message) 267 { 268 loggerDelegate.error(message); 269 } 270 271 277 public void error(Object message, Throwable t) 278 { 279 loggerDelegate.error(message, t); 280 } 281 282 287 public void fatal(Object message) 288 { 289 loggerDelegate.fatal(message); 290 } 291 292 298 public void fatal(Object message, Throwable t) 299 { 300 loggerDelegate.fatal(message, t); 301 } 302 303 310 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException 311 { 312 stream.defaultReadObject(); 314 315 if (pluginClass == null) 317 { 318 init(); 319 } 320 this.loggerDelegate = getDelegatePlugin(name); 321 } 322 323 329 public static Logger getLogger(String name) 330 { 331 return new Logger(name); 332 } 333 334 343 public static Logger getLogger(String name, String suffix) 344 { 345 return new Logger(name + "." + suffix); 346 } 347 348 355 public static Logger getLogger(Class clazz) 356 { 357 return new Logger(clazz.getName()); 358 } 359 360 369 public static Logger getLogger(Class clazz, String suffix) 370 { 371 return new Logger(clazz.getName() + "." + suffix); 372 } 373 374 380 protected static LoggerPlugin getDelegatePlugin(String name) 381 { 382 LoggerPlugin plugin = null; 383 try 384 { 385 plugin = (LoggerPlugin) pluginClass.newInstance(); 386 } 387 catch (Throwable e) 388 { 389 plugin = new NullLoggerPlugin(); 390 } 391 try 392 { 393 plugin.init(name); 394 } 395 catch (Throwable e) 396 { 397 String extraInfo = e.getMessage(); 398 System.err.println("Failed to initalize plugin: " + plugin 399 + (extraInfo != null ? ", cause: " + extraInfo : "")); 400 plugin = new NullLoggerPlugin(); 401 } 402 403 return plugin; 404 } 405 406 414 protected static void init() 415 { 416 try 417 { 418 if (pluginClassName == null) 420 { 421 pluginClassName = System.getProperty(PLUGIN_CLASS_PROP, LOG4J_PLUGIN_CLASS_NAME); 422 } 423 424 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 426 pluginClass = cl.loadClass(pluginClassName); 427 } 428 catch (Throwable e) 429 { 430 pluginClass = org.jboss.logging.NullLoggerPlugin.class; 432 } 433 } 434 } 435 | Popular Tags |