1 2 3 27 28 29 package org.apache.catalina.logger; 30 31 32 import java.beans.PropertyChangeSupport ; 33 import java.beans.PropertyChangeListener ; 34 import java.io.CharArrayWriter ; 35 import java.io.PrintWriter ; 36 import java.util.Set ; 37 import javax.servlet.ServletException ; 38 import javax.management.ObjectName ; 39 import javax.management.MBeanServer ; 40 import javax.management.MBeanRegistration ; 41 import org.apache.catalina.Container; 42 import org.apache.catalina.Logger; 43 import org.apache.catalina.Lifecycle; 44 import org.apache.catalina.LifecycleEvent; 45 import org.apache.catalina.LifecycleException; 46 import org.apache.catalina.LifecycleListener; 47 import org.apache.catalina.core.StandardEngine; 48 import org.apache.catalina.core.StandardHost; 49 import org.apache.catalina.core.StandardContext; 50 import org.apache.catalina.util.LifecycleSupport; 51 import com.sun.org.apache.commons.logging.Log; 52 import com.sun.org.apache.commons.logging.LogFactory; 53 import com.sun.org.apache.commons.modeler.Registry; 54 55 56 64 65 public class LoggerBase 66 implements Lifecycle, Logger, MBeanRegistration 67 { 68 private static Log log = LogFactory.getLog(LoggerBase.class); 69 70 72 73 76 protected Container container = null; 77 78 79 82 protected int debug = 0; 83 84 85 88 protected static final String info = 89 "org.apache.catalina.logger.LoggerBase/1.0"; 90 91 92 95 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 96 97 98 101 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 102 103 104 107 protected int verbosity = ERROR; 108 109 110 112 113 116 public Container getContainer() { 117 118 return (container); 119 120 } 121 122 123 128 public void setContainer(Container container) { 129 130 Container oldContainer = this.container; 131 this.container = container; 132 support.firePropertyChange("container", oldContainer, this.container); 133 134 } 135 136 137 140 public int getDebug() { 141 142 return (this.debug); 143 144 } 145 146 147 152 public void setDebug(int debug) { 153 154 this.debug = debug; 155 156 } 157 158 159 164 public String getInfo() { 165 166 return (info); 167 168 } 169 170 171 175 public int getVerbosity() { 176 177 return (this.verbosity); 178 179 } 180 181 182 188 public void setVerbosity(int verbosity) { 189 190 this.verbosity = verbosity; 191 192 } 193 194 195 201 public void setVerbosityLevel(String verbosity) { 202 203 if ("FATAL".equalsIgnoreCase(verbosity)) 204 this.verbosity = FATAL; 205 else if ("ERROR".equalsIgnoreCase(verbosity)) 206 this.verbosity = ERROR; 207 else if ("WARNING".equalsIgnoreCase(verbosity)) 208 this.verbosity = WARNING; 209 else if ("INFORMATION".equalsIgnoreCase(verbosity)) 210 this.verbosity = INFORMATION; 211 else if ("DEBUG".equalsIgnoreCase(verbosity)) 212 this.verbosity = DEBUG; 213 214 } 215 216 217 219 220 225 public void addPropertyChangeListener(PropertyChangeListener listener) { 226 227 support.addPropertyChangeListener(listener); 228 229 } 230 231 232 240 public void log(String msg) { 241 log.debug(msg); 242 } 243 244 245 256 public void log(Exception exception, String msg) { 257 258 log(msg, exception); 259 260 } 261 262 263 273 public void log(String msg, Throwable throwable) { 274 275 CharArrayWriter buf = new CharArrayWriter (); 276 PrintWriter writer = new PrintWriter (buf); 277 writer.println(msg); 278 throwable.printStackTrace(writer); 279 Throwable rootCause = null; 280 if (throwable instanceof LifecycleException) 281 rootCause = ((LifecycleException) throwable).getThrowable(); 282 else if (throwable instanceof ServletException ) 283 rootCause = ((ServletException ) throwable).getRootCause(); 284 if (rootCause != null) { 285 writer.println("----- Root Cause -----"); 286 rootCause.printStackTrace(writer); 287 } 288 log(buf.toString()); 289 290 } 291 292 293 302 public void log(String message, int verbosity) { 303 304 if (this.verbosity >= verbosity) 305 log(message); 306 307 } 308 309 310 320 public void log(String message, Throwable throwable, int verbosity) { 321 322 if (this.verbosity >= verbosity) 323 log(message, throwable); 324 325 } 326 327 328 333 public void removePropertyChangeListener(PropertyChangeListener listener) { 334 335 support.removePropertyChangeListener(listener); 336 337 } 338 339 protected String domain; 340 protected String host; 341 protected String path; 342 protected ObjectName oname; 343 protected ObjectName controller; 344 protected MBeanServer mserver; 345 346 public ObjectName getController() { 347 return controller; 348 } 349 350 public void setController(ObjectName controller) { 351 this.controller = controller; 352 } 353 354 public ObjectName getObjectName() { 355 return oname; 356 } 357 358 public String getDomain() { 359 return domain; 360 } 361 362 public ObjectName preRegister(MBeanServer server, 363 ObjectName name) throws Exception { 364 oname=name; 365 mserver=server; 366 if (name == null) { 368 return null; 369 } 370 domain=name.getDomain(); 371 host=name.getKeyProperty("host"); 372 path=name.getKeyProperty("path"); 373 log("preRegister with "+name); 374 if( container== null ) { 375 try { 377 ObjectName cname=null; 378 if( host == null ) { 379 cname=new ObjectName (domain +":type=Engine"); 381 } else if( path==null ) { 382 cname=new ObjectName (domain + 383 ":type=Host,host=" + host); 384 } else { 385 cname=new ObjectName (domain +":j2eeType=WebModule,name=//" + 386 host + "/" + path); 387 } 388 log.debug("Register with " + cname); 389 mserver.invoke(cname, "setLogger", new Object [] {this}, 390 new String [] {"org.apache.catalina.Logger"}); 391 } catch (Exception e) { 392 e.printStackTrace(); } 394 } 395 396 return name; 397 } 398 399 public void postRegister(Boolean registrationDone) { 400 } 401 402 public void preDeregister() throws Exception { 403 } 404 405 public void postDeregister() { 406 } 407 408 public void init() { 409 410 } 411 412 public void destroy() { 413 414 } 415 416 public ObjectName createObjectName() { 417 log("createObjectName with "+container); 418 try { 420 StandardEngine engine=null; 421 String suffix=""; 422 if( container instanceof StandardEngine ) { 423 engine=(StandardEngine)container; 424 } else if( container instanceof StandardHost ) { 425 engine=(StandardEngine)container.getParent(); 426 suffix=",host=" + container.getName(); 427 } else if( container instanceof StandardContext ) { 428 String path = ((StandardContext)container).getPath(); 429 if (path.equals("")) { 431 path = "/"; 432 } 433 engine=(StandardEngine)container.getParent().getParent(); 434 suffix= ",path=" + path + ",host=" + 435 container.getParent().getName(); 436 } else { 437 log.error("Unknown container " + container ); 438 } 439 if( engine != null ) { 440 oname=new ObjectName (engine.getDomain()+ ":type=Logger" + suffix); 441 } else { 442 log.error("Null engine !! " + container); 443 } 444 } catch (Throwable e) { 445 e.printStackTrace(); } 447 return oname; 448 } 449 450 451 453 458 public void addLifecycleListener(LifecycleListener listener) { 459 460 lifecycle.addLifecycleListener(listener); 461 462 } 463 464 465 469 public LifecycleListener[] findLifecycleListeners() { 470 471 return lifecycle.findLifecycleListeners(); 472 473 } 474 475 476 481 public void removeLifecycleListener(LifecycleListener listener) { 482 483 lifecycle.removeLifecycleListener(listener); 484 485 } 486 487 488 496 public void start() throws LifecycleException { 497 498 if ( getObjectName()==null ) { 500 ObjectName oname = createObjectName(); 501 try { 502 Registry.getRegistry().registerComponent(this, oname, null); 503 log.debug( "registering logger " + oname ); 504 } catch( Exception ex ) { 505 log.error( "Can't register logger " + oname, ex); 506 } 507 } 508 509 } 510 511 512 520 public void stop() throws LifecycleException { 521 522 if ( getObjectName()!=null ) { 524 ObjectName oname = createObjectName(); 525 try { 526 Registry.getRegistry().unregisterComponent(oname); 527 log.info( "unregistering logger " + oname ); 528 } catch( Exception ex ) { 529 log.error( "Can't unregister logger " + oname, ex); 530 } 531 } 532 } 533 534 } 535 | Popular Tags |