You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ak...@locus.apache.org on 2000/02/13 07:18:49 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/logging Logger.java TomcatLogger.java
akv 00/02/12 22:18:48
Added: src/share/org/apache/tomcat/logging Logger.java
TomcatLogger.java
Log:
Main implementation of the logging service.
Revision Changes Path
1.1 jakarta-tomcat/src/share/org/apache/tomcat/logging/Logger.java
Index: Logger.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.tomcat.logging;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
/**
* Interface for a logging object. A logging object provides mechanism
* for logging errors and messages that are of interest to someone who
* is trying to monitor the system.
*
* @author Anil Vijendran (akv@eng.sun.com)
* @since Tomcat 3.1
*/
public abstract class Logger {
/**
* Is this Log usable?
*/
public boolean isOpen() {
return this.sink != null;
}
/**
* Prints the log message on a specified logger.
*
* @param name the name of the logger.
* @param message the message to log.
* @param verbosityLevel what type of message is this?
* (WARNING/DEBUG/INFO etc)
*/
public static void log(String logName, String message,
int verbosityLevel)
{
Logger logger = getLogger(logName);
if (logger != null)
logger.log(message, verbosityLevel);
}
/**
* Prints the log message on a specified logger at the "default"
* log leve: INFORMATION
*
* @param name the name of the logger.
* @param message the message to log.
*/
public static void log(String logName, String message)
{
Logger logger = getLogger(logName);
if (logger != null)
logger.log(message);
}
/**
* Prints the log message.
*
* @param message the message to log.
* @param verbosityLevel what type of message is this?
* (WARNING/DEBUG/INFO etc)
*/
public final void log(String message, int verbosityLevel) {
if (matchVerbosityLevel(verbosityLevel))
realLog(message);
}
/**
* Prints the log message at the "default" log level: INFORMATION
*
* @param message the message to log.
*/
public final void log(String message) {
log(message, Logger.INFORMATION);
}
/**
* Prints log message and stack trace.
*
* @param message the message to log.
* @param t the exception that was thrown.
* @param verbosityLevel what type of message is this?
* (WARNING/DEBUG/INFO etc)
*/
public final void log(String message, Throwable t,
int verbosityLevel)
{
if (matchVerbosityLevel(verbosityLevel))
realLog(message, t);
}
public boolean matchVerbosityLevel(int verbosityLevel) {
return verbosityLevel <= getVerbosityLevel();
}
/**
* Subclasses implement these methods which are called by the
* log(..) methods internally.
*
* @param message the message to log.
*/
protected abstract void realLog(String message);
/**
* Subclasses implement these methods which are called by the
* log(..) methods internally.
*
* @param message the message to log.
* @param t the exception that was thrown.
*/
protected abstract void realLog(String message, Throwable t);
/**
* Flush the log.
*/
public abstract void flush();
/**
* Close the log.
*/
public synchronized void close() {
this.sink = null;
loggers.remove(getName());
}
/**
* Get name of this log channel.
*/
public String getName() {
return this.name;
}
/**
* Set name of this log channel.
*
* @param name Name of this logger.
*/
public void setName(String name) {
this.name = name;
// Once the name of this logger is set, we add it to the list
// of loggers...
putLogger(this);
}
/**
* Set the path name for the log output file.
*
* @param path The path to the log file.
*/
public void setPath(String path) {
try {
File file = new File(path);
if (!file.exists())
new File(file.getParent()).mkdirs();
this.sink = new FileWriter(path);
} catch (IOException ex) {
System.err.print("Unable to open log file: "+path+"! ");
System.err.println(" Using stderr as the default.");
this.sink = defaultSink;
}
}
/**
* Verbosity level codes.
*/
public static final int FATAL = Integer.MIN_VALUE;
public static final int ERROR = 1;
public static final int WARNING = 2;
public static final int INFORMATION = 3;
public static final int DEBUG = 4;
/**
* Set the verbosity level for this logger. This controls how the
* logs will be filtered.
*
* @param level one of the verbosity level codes.
*/
public void setVerbosityLevel(String level) {
if ("warning".equalsIgnoreCase(level))
this.level = WARNING;
else if ("fatal".equalsIgnoreCase(level))
this.level = FATAL;
else if ("error".equalsIgnoreCase(level))
this.level = ERROR;
else if ("information".equalsIgnoreCase(level))
this.level = INFORMATION;
else if ("debug".equalsIgnoreCase(level))
this.level = DEBUG;
}
/**
* Get the current verbosity level.
*/
public int getVerbosityLevel() {
return this.level;
}
/**
* Set the default output stream that is used by all logging
* channels.
*
* @param w the default output stream.
*/
public static void setDefaultSink(Writer w) {
defaultSink = w;
}
public static Logger getLogger(String name) {
return (Logger) loggers.get(name);
}
public static void putLogger(Logger logger) {
loggers.put(logger.getName(), logger);
}
public static void removeLogger(Logger logger) {
loggers.remove(logger.getName());
}
protected Writer sink = defaultSink;
protected String name;
protected static Writer defaultSink = new PrintWriter(System.err);
protected static Hashtable loggers = new Hashtable(5);
private int level = WARNING;
}
1.1 jakarta-tomcat/src/share/org/apache/tomcat/logging/TomcatLogger.java
Index: TomcatLogger.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.tomcat.logging;
import java.io.Writer;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.Date;
import org.apache.tomcat.util.Queue;
/**
* A real implementation of the Logger abstraction.
*
* @author Anil V (akv@eng.sun.com)
* @since Tomcat 3.1
*/
public class TomcatLogger extends Logger {
/**
* This is an entry that is created in response to every
* Logger.log(...) call.
*/
class LogEntry {
String logName;
long date;
String message;
Throwable t;
LogEntry(String message, Throwable t) {
this.date = System.currentTimeMillis();
this.message = message;
this.t = t;
}
/**
* Get the writer into which this log entry needs to be
* written into.
*/
Writer getWriter() {
return TomcatLogger.this.sink;
}
/**
* Format the log message nicely into a string.
*/
public String toString() {
StringWriter sw = new StringWriter();
PrintWriter w = new PrintWriter(sw);
w.print("<"+TomcatLogger.this.getName()+"> ");
w.print(new Date(date).toString());
w.print(' ');
if (message != null)
w.println(message);
if (t != null)
t.printStackTrace(w);
return sw.toString();
}
}
/**
* Just one daemon and one queue for all Logger instances..
*/
static LogDaemon logDaemon = null;
static Queue logQueue = null;
public TomcatLogger() {
if (logDaemon == null || logQueue == null) {
logQueue = new Queue();
logDaemon = new LogDaemon(logQueue);
logDaemon.start();
}
}
/**
* Adds a log message to the queue and returns immediately. The
* logger daemon thread will pick it up later and actually print
* it out.
*
* @param message the message to log.
*/
protected void realLog(String message) {
logQueue.put(new LogEntry(message, null));
}
/**
* Adds a log message and stack trace to the queue and returns
* immediately. The logger daemon thread will pick it up later and
* actually print it out.
*
* @param message the message to log.
* @param t the exception that was thrown.
*/
protected void realLog(String message, Throwable t) {
logQueue.put(new LogEntry(message, t));
}
/**
* Flush the log.
*/
public void flush() {
logDaemon.flush();
}
}
/**
* The daemon thread that looks in a queue and if it is not empty
* writes out everything in the queue to the sink.
*/
class LogDaemon extends Thread {
LogDaemon(Queue logQueue) {
this.logQueue = logQueue;
setDaemon(true);
}
Runnable flusher = new Runnable() {
public void run() {
do {
TomcatLogger.LogEntry logEntry = (TomcatLogger.LogEntry) logQueue.pull();
Writer writer = logEntry.getWriter();
if (writer != null)
try {
writer.write(logEntry.toString());
writer.flush();
} catch (Exception ex) { // IOException
ex.printStackTrace();
}
} while (!logQueue.isEmpty());
}
};
public void run() {
while (true)
flusher.run();
}
public void flush() {
Thread workerThread = new Thread(flusher);
workerThread.start();
}
private Queue logQueue;
}