001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/logger/LoggerBase.java,v 1.5 2002/01/25 20:12:20 amyroh Exp $
003: * $Revision: 1.5 $
004: * $Date: 2002/01/25 20:12:20 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.logger;
065:
066: import java.beans.PropertyChangeSupport;
067: import java.beans.PropertyChangeListener;
068: import java.io.CharArrayWriter;
069: import java.io.PrintWriter;
070: import javax.servlet.ServletException;
071: import org.apache.catalina.Container;
072: import org.apache.catalina.LifecycleException;
073: import org.apache.catalina.Logger;
074:
075: /**
076: * Convenience base class for <b>Logger</b> implementations. The only
077: * method that must be implemented is <code>log(String msg)</code>, plus
078: * any property setting and lifecycle methods required for configuration.
079: *
080: * @author Craig R. McClanahan
081: * @version $Revision: 1.5 $ $Date: 2002/01/25 20:12:20 $
082: */
083:
084: public abstract class LoggerBase implements Logger {
085:
086: // ----------------------------------------------------- Instance Variables
087:
088: /**
089: * The Container with which this Logger has been associated.
090: */
091: protected Container container = null;
092:
093: /**
094: * The debugging detail level for this component.
095: */
096: protected int debug = 0;
097:
098: /**
099: * The descriptive information about this implementation.
100: */
101: protected static final String info = "org.apache.catalina.logger.LoggerBase/1.0";
102:
103: /**
104: * The property change support for this component.
105: */
106: protected PropertyChangeSupport support = new PropertyChangeSupport(
107: this );
108:
109: /**
110: * The verbosity level for above which log messages may be filtered.
111: */
112: protected int verbosity = ERROR;
113:
114: // ------------------------------------------------------------- Properties
115:
116: /**
117: * Return the Container with which this Logger has been associated.
118: */
119: public Container getContainer() {
120:
121: return (container);
122:
123: }
124:
125: /**
126: * Set the Container with which this Logger has been associated.
127: *
128: * @param container The associated Container
129: */
130: public void setContainer(Container container) {
131:
132: Container oldContainer = this .container;
133: this .container = container;
134: support.firePropertyChange("container", oldContainer,
135: this .container);
136:
137: }
138:
139: /**
140: * Return the debugging detail level for this component.
141: */
142: public int getDebug() {
143:
144: return (this .debug);
145:
146: }
147:
148: /**
149: * Set the debugging detail level for this component.
150: *
151: * @param debug The new debugging detail level
152: */
153: public void setDebug(int debug) {
154:
155: this .debug = debug;
156:
157: }
158:
159: /**
160: * Return descriptive information about this Logger implementation and
161: * the corresponding version number, in the format
162: * <code><description>/<version></code>.
163: */
164: public String getInfo() {
165:
166: return (info);
167:
168: }
169:
170: /**
171: * Return the verbosity level of this logger. Messages logged with a
172: * higher verbosity than this level will be silently ignored.
173: */
174: public int getVerbosity() {
175:
176: return (this .verbosity);
177:
178: }
179:
180: /**
181: * Set the verbosity level of this logger. Messages logged with a
182: * higher verbosity than this level will be silently ignored.
183: *
184: * @param verbosity The new verbosity level
185: */
186: public void setVerbosity(int verbosity) {
187:
188: this .verbosity = verbosity;
189:
190: }
191:
192: /**
193: * Set the verbosity level of this logger. Messages logged with a
194: * higher verbosity than this level will be silently ignored.
195: *
196: * @param verbosityLevel The new verbosity level, as a string
197: */
198: public void setVerbosityLevel(String verbosity) {
199:
200: if ("FATAL".equalsIgnoreCase(verbosity))
201: this .verbosity = FATAL;
202: else if ("ERROR".equalsIgnoreCase(verbosity))
203: this .verbosity = ERROR;
204: else if ("WARNING".equalsIgnoreCase(verbosity))
205: this .verbosity = WARNING;
206: else if ("INFORMATION".equalsIgnoreCase(verbosity))
207: this .verbosity = INFORMATION;
208: else if ("DEBUG".equalsIgnoreCase(verbosity))
209: this .verbosity = DEBUG;
210:
211: }
212:
213: // --------------------------------------------------------- Public Methods
214:
215: /**
216: * Add a property change listener to this component.
217: *
218: * @param listener The listener to add
219: */
220: public void addPropertyChangeListener(
221: PropertyChangeListener listener) {
222:
223: support.addPropertyChangeListener(listener);
224:
225: }
226:
227: /**
228: * Writes the specified message to a servlet log file, usually an event
229: * log. The name and type of the servlet log is specific to the
230: * servlet container. This message will be logged unconditionally.
231: *
232: * @param message A <code>String</code> specifying the message to be
233: * written to the log file
234: */
235: public abstract void log(String msg);
236:
237: /**
238: * Writes the specified exception, and message, to a servlet log file.
239: * The implementation of this method should call
240: * <code>log(msg, exception)</code> instead. This method is deprecated
241: * in the ServletContext interface, but not deprecated here to avoid
242: * many useless compiler warnings. This message will be logged
243: * unconditionally.
244: *
245: * @param exception An <code>Exception</code> to be reported
246: * @param msg The associated message string
247: */
248: public void log(Exception exception, String msg) {
249:
250: log(msg, exception);
251:
252: }
253:
254: /**
255: * Writes an explanatory message and a stack trace for a given
256: * <code>Throwable</code> exception to the servlet log file. The name
257: * and type of the servlet log file is specific to the servlet container,
258: * usually an event log. This message will be logged unconditionally.
259: *
260: * @param msg A <code>String</code> that describes the error or
261: * exception
262: * @param throwable The <code>Throwable</code> error or exception
263: */
264: public void log(String msg, Throwable throwable) {
265:
266: CharArrayWriter buf = new CharArrayWriter();
267: PrintWriter writer = new PrintWriter(buf);
268: writer.println(msg);
269: throwable.printStackTrace(writer);
270: Throwable rootCause = null;
271: if (throwable instanceof LifecycleException)
272: rootCause = ((LifecycleException) throwable).getThrowable();
273: else if (throwable instanceof ServletException)
274: rootCause = ((ServletException) throwable).getRootCause();
275: if (rootCause != null) {
276: writer.println("----- Root Cause -----");
277: rootCause.printStackTrace(writer);
278: }
279: log(buf.toString());
280:
281: }
282:
283: /**
284: * Writes the specified message to the servlet log file, usually an event
285: * log, if the logger is set to a verbosity level equal to or higher than
286: * the specified value for this message.
287: *
288: * @param message A <code>String</code> specifying the message to be
289: * written to the log file
290: * @param verbosity Verbosity level of this message
291: */
292: public void log(String message, int verbosity) {
293:
294: if (this .verbosity >= verbosity)
295: log(message);
296:
297: }
298:
299: /**
300: * Writes the specified message and exception to the servlet log file,
301: * usually an event log, if the logger is set to a verbosity level equal
302: * to or higher than the specified value for this message.
303: *
304: * @param message A <code>String</code> that describes the error or
305: * exception
306: * @param throwable The <code>Throwable</code> error or exception
307: * @param verbosity Verbosity level of this message
308: */
309: public void log(String message, Throwable throwable, int verbosity) {
310:
311: if (this .verbosity >= verbosity)
312: log(message, throwable);
313:
314: }
315:
316: /**
317: * Remove a property change listener from this component.
318: *
319: * @param listener The listener to remove
320: */
321: public void removePropertyChangeListener(
322: PropertyChangeListener listener) {
323:
324: support.removePropertyChangeListener(listener);
325:
326: }
327:
328: }
|