1 23 24 package com.sun.enterprise.web.logger; 25 26 import java.beans.PropertyChangeSupport ; 27 import java.beans.PropertyChangeListener ; 28 import java.io.CharArrayWriter ; 29 import java.io.PrintWriter ; 30 import javax.servlet.ServletException ; 31 import org.apache.catalina.Container; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.Logger; 34 35 42 43 abstract class LoggerBase implements Logger { 44 45 47 50 protected Container container = null; 51 52 55 protected static final String info = 56 "com.sun.enterprise.web.logger.LoggerBase/1.0"; 57 58 61 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 62 63 65 68 public Container getContainer() { 69 return (container); 70 } 71 72 77 public void setContainer(Container container) { 78 79 Container oldContainer = this.container; 80 this.container = container; 81 support.firePropertyChange("container", oldContainer, this.container); 82 } 83 84 89 public String getInfo() { 90 return (info); 91 } 92 93 96 public int getVerbosity() { 97 return -1; 99 } 100 101 106 public void setVerbosity(int verbosity) { 107 } 109 110 112 117 public void addPropertyChangeListener(PropertyChangeListener listener) { 118 support.addPropertyChangeListener(listener); 119 } 120 121 129 public void log(String msg) { 130 write(msg, DEBUG); 131 } 132 133 144 public void log(Exception exception, String msg) { 145 log(msg, exception); 146 } 147 148 158 public void log(String msg, Throwable throwable) { 159 write(msg, throwable, ERROR); 160 } 161 162 171 public void log(String message, int verbosity) { 172 write(message, verbosity); 173 } 174 175 185 public void log(String message, Throwable throwable, int verbosity) { 186 write(message, throwable, verbosity); 187 } 188 189 194 public void removePropertyChangeListener(PropertyChangeListener listener) { 195 support.removePropertyChangeListener(listener); 196 } 197 198 protected void write(String msg, Throwable throwable, int verbosity) { 199 CharArrayWriter buf = new CharArrayWriter (); 200 PrintWriter writer = new PrintWriter (buf); 201 writer.println(msg); 202 throwable.printStackTrace(writer); 203 Throwable rootCause = null; 204 if (throwable instanceof LifecycleException) 205 rootCause = ((LifecycleException) throwable).getThrowable(); 206 else if (throwable instanceof ServletException ) 207 rootCause = ((ServletException ) throwable).getRootCause(); 208 if (rootCause != null) { 209 writer.println("----- Root Cause -----"); 210 rootCause.printStackTrace(writer); 211 } 212 write(buf.toString(), verbosity); 213 } 214 215 218 protected abstract void write(String msg, int verbosity); 219 } 220 | Popular Tags |