From 9a529044cbd5ccc256eb571b976cf7b1d38ec7e4 Mon Sep 17 00:00:00 2001 From: Scott Sanders Date: Sun, 3 Feb 2002 01:31:29 +0000 Subject: [PATCH] Moved implementation classes to impl package, as per Costin M. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138852 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/logging/impl/Jdk14Logger.java | 305 ++++++++++ .../logging/impl/Log4JCategoryLog.java | 248 ++++++++ .../commons/logging/impl/LogKitLogger.java | 274 +++++++++ .../apache/commons/logging/impl/NoOpLog.java | 150 +++++ .../commons/logging/impl/SimpleLog.java | 539 ++++++++++++++++++ 5 files changed, 1516 insertions(+) create mode 100644 src/java/org/apache/commons/logging/impl/Jdk14Logger.java create mode 100644 src/java/org/apache/commons/logging/impl/Log4JCategoryLog.java create mode 100644 src/java/org/apache/commons/logging/impl/LogKitLogger.java create mode 100644 src/java/org/apache/commons/logging/impl/NoOpLog.java create mode 100644 src/java/org/apache/commons/logging/impl/SimpleLog.java diff --git a/src/java/org/apache/commons/logging/impl/Jdk14Logger.java b/src/java/org/apache/commons/logging/impl/Jdk14Logger.java new file mode 100644 index 0000000..3165a07 --- /dev/null +++ b/src/java/org/apache/commons/logging/impl/Jdk14Logger.java @@ -0,0 +1,305 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Jdk14Logger.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + * $Revision: 1.1 $ + * $Date: 2002/02/03 01:31:29 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2001 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", "Commons", 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 + * . + * + */ + + +package org.apache.commons.logging.impl; + + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.logging.Log; + + +/** + *

Implementation of the org.apache.commons.logging.Log + * interfaces that wraps the standard JDK logging mechanisms that were + * introduced in the Merlin release (JDK 1.4).

+ * + * @author Scott Sanders + * @author Berin Loritsch + * @author Peter Donald + * @version $Revision: 1.1 $ $Date: 2002/02/03 01:31:29 $ + */ + +public final class Jdk14Logger implements Log { + + + // ----------------------------------------------------------- Constructors + + + /** + * Construct a named instance of this Logger. + * + * @param name Name of the logger to be constructed + */ + public Jdk14Logger(String name) { + + logger = Logger.getLogger(name); + logger.setUseParentHandlers(true); + logger.setLevel(Level.INFO); + + } + + + // ----------------------------------------------------- Instance Variables + + + /** + * The underlying Logger implementation we are using. + */ + protected Logger logger = null; + + + // --------------------------------------------------------- Public Methods + + + /** + * Log a message with debug log level. + */ + public void debug(Object message) { + + logger.log(Level.FINE, message.toString()); + + } + + + /** + * Log a message and exception with debug log level. + */ + public void debug(Object message, Throwable exception) { + + logger.log(Level.FINE, message.toString(), exception); + + } + + + /** + * Log a message with error log level. + */ + public void error(Object message) { + + logger.log(Level.SEVERE, message.toString()); + + } + + + /** + * Log a message and exception with error log level. + */ + public void error(Object message, Throwable exception) { + + logger.log(Level.SEVERE, message.toString(), exception); + + } + + + /** + * Log a message with fatal log level. + */ + public void fatal(Object message) { + + logger.log(Level.SEVERE, message.toString()); + + } + + + /** + * Log a message and exception with fatal log level. + */ + public void fatal(Object message, Throwable exception) { + + logger.log(Level.SEVERE, message.toString(), exception); + + } + + + /** + * Return the native Logger instance we are using. + */ + public Logger getLogger() { + + return (this.logger); + + } + + + /** + * Log a message with info log level. + */ + public void info(Object message) { + + logger.log(Level.INFO, message.toString()); + + } + + + /** + * Log a message and exception with info log level. + */ + public void info(Object message, Throwable exception) { + + logger.log(Level.INFO, message.toString(), exception); + + } + + + /** + * Is debug logging currently enabled? + */ + public boolean isDebugEnabled() { + + return (logger.isLoggable(Level.FINE)); + + } + + + /** + * Is error logging currently enabled? + */ + public boolean isErrorEnabled() { + + return (logger.isLoggable(Level.SEVERE)); + + } + + + /** + * Is fatal logging currently enabled? + */ + public boolean isFatalEnabled() { + + return (logger.isLoggable(Level.SEVERE)); + + } + + + /** + * Is info logging currently enabled? + */ + public boolean isInfoEnabled() { + + return (logger.isLoggable(Level.INFO)); + + } + + + /** + * Is tace logging currently enabled? + */ + public boolean isTraceEnabled() { + + return (logger.isLoggable(Level.FINEST)); + + } + + + /** + * Is warning logging currently enabled? + */ + public boolean isWarnEnabled() { + + return (logger.isLoggable(Level.WARNING)); + + } + + + /** + * Log a message with trace log level. + */ + public void trace(Object message) { + + logger.log(Level.FINEST, message.toString()); + + } + + + /** + * Log a message and exception with trace log level. + */ + public void trace(Object message, Throwable exception) { + + logger.log(Level.FINEST, message.toString(), exception); + + } + + + /** + * Log a message with warn log level. + */ + public void warn(Object message) { + + logger.log(Level.WARNING, message.toString()); + + } + + + /** + * Log a message and exception with warn log level. + */ + public void warn(Object message, Throwable exception) { + + logger.log(Level.WARNING, message.toString(), exception); + + } + + +} diff --git a/src/java/org/apache/commons/logging/impl/Log4JCategoryLog.java b/src/java/org/apache/commons/logging/impl/Log4JCategoryLog.java new file mode 100644 index 0000000..0e68d86 --- /dev/null +++ b/src/java/org/apache/commons/logging/impl/Log4JCategoryLog.java @@ -0,0 +1,248 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Attic/Log4JCategoryLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + * $Revision: 1.1 $ + * $Date: 2002/02/03 01:31:29 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2002 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", "Commons", 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 + * . + * + */ + + +package org.apache.commons.logging.impl; + +import org.apache.log4j.Category; +import org.apache.log4j.Priority; +import org.apache.commons.logging.Log; + +/** + *

Implementation of {@link Log} that maps directly to a Log4J + * Category. Initial configuration of the corresponding + * Category instances should be done in the usual manner, as outlined in + * the Log4J documentation.

+ * + * @author Scott Sanders + * @author Rod Waldhoff + * @author Robert Burrell Donkin + * @version $Id: Log4JCategoryLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + */ +public final class Log4JCategoryLog implements Log { + + + // ------------------------------------------------------------- Attributes + + + /** Log to this category */ + Category category = null; + + + // ------------------------------------------------------------ Constructor + + + /** + * Base constructor + */ + public Log4JCategoryLog(String name) { + category = Category.getInstance(name); + } + + + // ---------------------------------------------------------- Implmentation + + + /** + * Log a message to the Log4j Category with TRACE priority. + * Currently logs to DEBUG level in Log4J. + */ + public void trace(Object message) { + category.debug(message); + } + + + /** + * Log an error to the Log4j Category with TRACE priority. + * Currently logs to DEBUG level in Log4J. + */ + public void trace(Object message, Throwable t) { + category.debug(message,t); + } + + + /** + * Log a message to the Log4j Category with DEBUG priority. + */ + public void debug(Object message) { + category.debug(message); + } + + + /** + * Log an error to the Log4j Category with DEBUG priority. + */ + public void debug(Object message, Throwable t) { + category.debug(message,t); + } + + + /** + * Log a message to the Log4j Category with INFO priority. + */ + public void info(Object message) { + category.info(message); + } + + + /** + * Log an error to the Log4j Category with INFO priority. + */ + public void info(Object message, Throwable t) { + category.info(message,t); + } + + + /** + * Log a message to the Log4j Category with WARN priority. + */ + public void warn(Object message) { + category.warn(message); + } + + + /** + * Log an error to the Log4j Category with WARN priority. + */ + public void warn(Object message, Throwable t) { + category.warn(message,t); + } + + + /** + * Log a message to the Log4j Category with ERROR priority. + */ + public void error(Object message) { + category.error(message); + } + + + /** + * Log an error to the Log4j Category with ERROR priority. + */ + public void error(Object message, Throwable t) { + category.error(message,t); + } + + + /** + * Log a message to the Log4j Category with FATAL priority. + */ + public void fatal(Object message) { + category.fatal(message); + } + + + /** + * Log an error to the Log4j Category with FATAL priority. + */ + public void fatal(Object message, Throwable t) { + category.fatal(message,t); + } + + + /** + * Check whether the Log4j Category used is enabled for DEBUG priority. + */ + public boolean isDebugEnabled() { + return category.isDebugEnabled(); + } + + + /** + * Check whether the Log4j Category used is enabled for ERROR priority. + */ + public boolean isErrorEnabled() { + return category.isEnabledFor(Priority.ERROR); + } + + + /** + * Check whether the Log4j Category used is enabled for FATAL priority. + */ + public boolean isFatalEnabled() { + return category.isEnabledFor(Priority.FATAL); + } + + + /** + * Check whether the Log4j Category used is enabled for INFO priority. + */ + public boolean isInfoEnabled() { + return category.isInfoEnabled(); + } + + + /** + * Check whether the Log4j Category used is enabled for TRACE priority. + * For Log4J, this returns the value of isDebugEnabled() + */ + public boolean isTraceEnabled() { + return category.isDebugEnabled(); + } + + /** + * Check whether the Log4j Category used is enabled for WARN priority. + */ + public boolean isWarnEnabled() { + return category.isEnabledFor(Priority.WARN); + } +} diff --git a/src/java/org/apache/commons/logging/impl/LogKitLogger.java b/src/java/org/apache/commons/logging/impl/LogKitLogger.java new file mode 100644 index 0000000..2b8cbee --- /dev/null +++ b/src/java/org/apache/commons/logging/impl/LogKitLogger.java @@ -0,0 +1,274 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/LogKitLogger.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + * $Revision: 1.1 $ + * $Date: 2002/02/03 01:31:29 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2002 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", "Commons", 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 + * . + * + */ + + +package org.apache.commons.logging.impl; + +import org.apache.log.Logger; +import org.apache.log.Hierarchy; +import org.apache.commons.logging.Log; + +/** + *

Implementation of org.apache.commons.logging.Log + * that wraps the jakarta-avalon-logkit + * logging system. Configuration of LogKit is left to the user.

+ * + *

LogKit accepts only String messages. + * Therefore, this implementation converts object messages into strings + * by called their toString() method before logging them.

+ * + * @author Scott Sanders + * @author Robert Burrell Donkin * + * @version $Id: LogKitLogger.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + */ + +public final class LogKitLogger implements Log { + + + // ------------------------------------------------------------- Attributes + + + /** Logging goes to this LogKit logger */ + protected Logger logger = null; + + + // ------------------------------------------------------------ Constructor + + + /** + * Construct LogKitLogger which wraps the LogKit + * logger with given name. + * + * @param name log name + */ + public LogKitLogger(String name) { + logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name); + } + + + // ----------------------------------------------------- Log Implementation + + + /** + * Log message to LogKit logger with DEBUG priority. + */ + public void trace(Object message) { + debug(message); + } + + + /** + * Log error to LogKit logger with DEBUG priority. + */ + public void trace(Object message, Throwable t) { + debug(message, t); + } + + + /** + * Log message to LogKit logger with DEBUG priority. + */ + public void debug(Object message) { + if (message != null) { + logger.debug(message.toString()); + } + } + + + /** + * Log error to LogKit logger with DEBUG priority. + */ + public void debug(Object message, Throwable t) { + if (message != null) { + logger.debug(message.toString(), t); + } + } + + + /** + * Log message to LogKit logger with INFO priority. + */ + public void info(Object message) { + if (message != null) { + logger.info(message.toString()); + } + } + + + /** + * Log error to LogKit logger with INFO priority. + */ + public void info(Object message, Throwable t) { + if (message != null) { + logger.info(message.toString(), t); + } + } + + + /** + * Log message to LogKit logger with WARN priority. + */ + public void warn(Object message) { + if (message != null) { + logger.warn(message.toString()); + } + } + + + /** + * Log error to LogKit logger with WARN priority. + */ + public void warn(Object message, Throwable t) { + if (message != null) { + logger.warn(message.toString(), t); + } + } + + + /** + * Log message to LogKit logger with ERROR priority. + */ + public void error(Object message) { + if (message != null) { + logger.error(message.toString()); + } + } + + + /** + * Log error to LogKit logger with ERROR priority. + */ + public void error(Object message, Throwable t) { + if (message != null) { + logger.error(message.toString(), t); + } + } + + + /** + * Log message to LogKit logger with FATAL_ERROR priority. + */ + public void fatal(Object message) { + if (message != null) { + logger.fatalError(message.toString()); + } + } + + + /** + * Log error to LogKit logger with FATAL_ERROR priority. + */ + public void fatal(Object message, Throwable t) { + if (message != null) { + logger.fatalError(message.toString(), t); + } + } + + + /** + * Check whether the LogKit logger will log messages of priority DEBUG. + */ + public boolean isDebugEnabled() { + return logger.isDebugEnabled(); + } + + + /** + * Check whether the LogKit logger will log messages of priority ERROR. + */ + public boolean isErrorEnabled() { + return logger.isErrorEnabled(); + } + + + /** + * Check whether the LogKit logger will log messages of priority FATAL_ERROR. + */ + public boolean isFatalEnabled() { + return logger.isFatalErrorEnabled(); + } + + + /** + * Check whether the LogKit logger will log messages of priority INFO. + */ + public boolean isInfoEnabled() { + return logger.isInfoEnabled(); + } + + + /** + * Check whether the LogKit logger will log messages of priority DEBUG. + */ + public boolean isTraceEnabled() { + return logger.isDebugEnabled(); + } + + + /** + * Check whether the LogKit logger will log messages of priority WARN. + */ + public boolean isWarnEnabled() { + return logger.isWarnEnabled(); + } + + +} diff --git a/src/java/org/apache/commons/logging/impl/NoOpLog.java b/src/java/org/apache/commons/logging/impl/NoOpLog.java new file mode 100644 index 0000000..ac7aaa8 --- /dev/null +++ b/src/java/org/apache/commons/logging/impl/NoOpLog.java @@ -0,0 +1,150 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/NoOpLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + * $Revision: 1.1 $ + * $Date: 2002/02/03 01:31:29 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2002 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", "Commons", 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 + * . + * + */ + + +package org.apache.commons.logging.impl; + + +import org.apache.commons.logging.Log; + + +/** + *

Default implementation of Log that throws away all messages. No + * configurable system properties are supported.

+ * + * @author Scott Sanders + * @author Rod Waldhoff + * @version $Id: NoOpLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + */ +public final class NoOpLog implements Log { + + /** Convenience constructor */ + public NoOpLog() { } + /** Base constructor */ + public NoOpLog(String name) { } + /** Do nothing */ + public void trace(Object message) { } + /** Do nothing */ + public void trace(Object message, Throwable t) { } + /** Do nothing */ + public void debug(Object message) { } + /** Do nothing */ + public void debug(Object message, Throwable t) { } + /** Do nothing */ + public void info(Object message) { } + /** Do nothing */ + public void info(Object message, Throwable t) { } + /** Do nothing */ + public void warn(Object message) { } + /** Do nothing */ + public void warn(Object message, Throwable t) { } + /** Do nothing */ + public void error(Object message) { } + /** Do nothing */ + public void error(Object message, Throwable t) { } + /** Do nothing */ + public void fatal(Object message) { } + /** Do nothing */ + public void fatal(Object message, Throwable t) { } + + /** + * Debug is never enabled. + * + * @return false + */ + public final boolean isDebugEnabled() { return false; } + + /** + * Error is never enabled. + * + * @return false + */ + public final boolean isErrorEnabled() { return false; } + + /** + * Fatal is never enabled. + * + * @return false + */ + public final boolean isFatalEnabled() { return false; } + + /** + * Info is never enabled. + * + * @return false + */ + public final boolean isInfoEnabled() { return false; } + + /** + * Trace is never enabled. + * + * @return false + */ + public final boolean isTraceEnabled() { return false; } + + /** + * Warning is never enabled. + * + * @return false + */ + public final boolean isWarnEnabled() { return false; } + +} diff --git a/src/java/org/apache/commons/logging/impl/SimpleLog.java b/src/java/org/apache/commons/logging/impl/SimpleLog.java new file mode 100644 index 0000000..91861fe --- /dev/null +++ b/src/java/org/apache/commons/logging/impl/SimpleLog.java @@ -0,0 +1,539 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/SimpleLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + * $Revision: 1.1 $ + * $Date: 2002/02/03 01:31:29 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2002 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", "Commons", 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 + * . + * + */ + + +package org.apache.commons.logging.impl; + +import java.util.Properties; +import java.util.Enumeration; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.text.DateFormat; +import java.util.Date; + +import org.apache.commons.logging.Log; + +/** + *

Simple implementation of Log that sends all enabled log messages, + * for all defined loggers, to System.out. The following system properties + * are supported to configure the behavior of this logger:

+ *
    + *
  • org.apache.commons.logging.simplelog.defaultlog - + * Default logging detail level for all instances of SimpleLog. + * Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). + * If not specified, defaults to "error".
  • + *
  • org.apache.commons.logging.simplelog.log.xxxxx - + * Logging detail level for a SimpleLog instance named "xxxxx". + * Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). + * If not specified, the default logging detail level is used.
  • + *
  • org.apache.commons.logging.simplelog.showlogname - + * Set to true if you want the Log instance name to be + * included in output messages.
  • + *
  • org.apache.commons.logging.simplelog.showdatetime - + * Set to true if you want the current date and time + * to be included in output messages.
  • + *
+ * + *

In addition to looking for system properties with the names specified + * above, this implementation also checks for a class loader resource named + * "simplelog.properties", and includes any matching definitions + * from this resource (if it exists).

+ * + * @author Scott Sanders + * @author Rod Waldhoff + * @author Robert Burrell Donkin + * + * @version $Id: SimpleLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ + */ +public class SimpleLog implements Log { + + + // ------------------------------------------------------- Class Attributes + + /** All system properties used by Simple start with this */ + static protected final String systemPrefix = + "org.apache.commons.logging.simplelog."; + + /** All system properties which start with {@link #systemPrefix} */ + static protected final Properties simpleLogProps = new Properties(); + /** Include the instance name in the log message? */ + static protected boolean showLogName = false; + /** Include the current time in the log message */ + static protected boolean showDateTime = false; + /** Used to format times */ + static protected DateFormat dateFormatter = null; + + // ---------------------------------------------------- Log Level Constants + + + /** "Trace" level logging. */ + public static final int LOG_LEVEL_TRACE = 1; + /** "Debug" level logging. */ + public static final int LOG_LEVEL_DEBUG = 2; + /** "Info" level logging. */ + public static final int LOG_LEVEL_INFO = 3; + /** "Warn" level logging. */ + public static final int LOG_LEVEL_WARN = 4; + /** "Error" level logging. */ + public static final int LOG_LEVEL_ERROR = 5; + /** "Fatal" level logging. */ + public static final int LOG_LEVEL_FATAL = 6; + + /** Enable all logging levels */ + public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1); + + /** Enable no logging levels */ + public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1); + + // ------------------------------------------------------------ Initializer + + // initialize class attributes + static { + // add all system props that start with the specified prefix + Enumeration enum = System.getProperties().propertyNames(); + while(enum.hasMoreElements()) { + String name = (String)(enum.nextElement()); + if(null != name && name.startsWith(systemPrefix)) { + simpleLogProps.setProperty(name,System.getProperty(name)); + } + } + + // add props from the resource simplelog.properties + InputStream in = + ClassLoader.getSystemResourceAsStream("simplelog.properties"); + if(null != in) { + try { + simpleLogProps.load(in); + in.close(); + } catch(java.io.IOException e) { + // ignored + } + } + + showLogName = "true".equalsIgnoreCase( + simpleLogProps.getProperty( + systemPrefix + "showlogname","true")); + + showDateTime = "true".equalsIgnoreCase( + simpleLogProps.getProperty( + systemPrefix + "showdatetime","true")); + + if(showDateTime) { + dateFormatter = new SimpleDateFormat( + simpleLogProps.getProperty( + systemPrefix + "dateformat","yyyy/MM/dd HH:mm:ss:SSS zzz")); + } + } + + + // ------------------------------------------------------------- Attributes + + /** The name of this simple log instance */ + protected String logName = null; + /** The current log level */ + protected int currentLogLevel; + + + // ------------------------------------------------------------ Constructor + + /** + * Construct a simple log with given name. + * + * @param name log name + */ + public SimpleLog(String name) { + + logName = name; + + // set initial log level + // set default log level to ERROR + setLevel(SimpleLog.LOG_LEVEL_ERROR); + + // set log level from properties + String lvl = simpleLogProps.getProperty(systemPrefix + "log." + logName); + int i = String.valueOf(name).lastIndexOf("."); + while(null == lvl && i > -1) { + name = name.substring(0,i); + lvl = simpleLogProps.getProperty(systemPrefix + "log." + name); + i = String.valueOf(name).lastIndexOf("."); + } + + if(null == lvl) { + lvl = simpleLogProps.getProperty(systemPrefix + "defaultlog"); + } + + if("all".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_ALL); + } else if("trace".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_TRACE); + } else if("debug".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_DEBUG); + } else if("info".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_INFO); + } else if("warn".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_WARN); + } else if("error".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_ERROR); + } else if("fatal".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_FATAL); + } else if("off".equalsIgnoreCase(lvl)) { + setLevel(SimpleLog.LOG_LEVEL_OFF); + } + + } + + + // -------------------------------------------------------- Properties + + /** + *

Set logging level.

+ * + * @param level new logging level + */ + public void setLevel(int currentLogLevel) { + + this.currentLogLevel = currentLogLevel; + } + + + /** + *

Get logging level.

+ */ + public int getLevel() { + + return currentLogLevel; + } + + + // -------------------------------------------------------- Logging Methods + + + /** + *

Do the actual logging. + * This method assembles the message + * and then prints to System.out.

+ */ + protected void log(int type, Object message, Throwable t) { + // use a string buffer for better performance + StringBuffer buf = new StringBuffer(); + + // append date-time if so configured + if(showDateTime) { + buf.append(dateFormatter.format(new Date())); + buf.append(" "); + } + + // append a readable representation of the log leve + switch(type) { + case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break; + case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break; + case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO] "); break; + case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN] "); break; + case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break; + case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break; + } + + // append the name of the log instance if so configured + if(showLogName) { + buf.append(String.valueOf(logName)).append(" - "); + } + + // append the message + buf.append(String.valueOf(message)); + + // append stack trace if not null + if(t != null) { + buf.append(" <"); + buf.append(t.toString()); + buf.append(">"); + t.printStackTrace(); + } + + // print to System.out + System.out.println(buf.toString()); + } + + + /** + * Is the given log level currently enabled? + * + * @param logLevel is this level enabled? + */ + protected boolean isLevelEnabled(int logLevel) { + // log level are numerically ordered so can use simple numeric + // comparison + return (logLevel >= currentLogLevel); + } + + + // -------------------------------------------------------- Log Implementation + + + /** + *

Log a message with debug log level.

+ */ + public final void debug(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { + log(SimpleLog.LOG_LEVEL_DEBUG, message, null); + } + } + + + /** + *

Log an error with debug log level.

+ */ + public final void debug(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { + log(SimpleLog.LOG_LEVEL_DEBUG, message, t); + } + } + + + /** + *

Log a message with debug log level.

+ */ + public final void trace(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { + log(SimpleLog.LOG_LEVEL_TRACE, message, null); + } + } + + + /** + *

Log an error with debug log level.

+ */ + public final void trace(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { + log(SimpleLog.LOG_LEVEL_TRACE, message, t); + } + } + + + /** + *

Log a message with info log level.

+ */ + public final void info(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { + log(SimpleLog.LOG_LEVEL_INFO,message,null); + } + } + + + /** + *

Log an error with info log level.

+ */ + public final void info(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { + log(SimpleLog.LOG_LEVEL_INFO, message, t); + } + } + + + /** + *

Log a message with warn log level.

+ */ + public final void warn(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { + log(SimpleLog.LOG_LEVEL_WARN, message, null); + } + } + + + /** + *

Log an error with warn log level.

+ */ + public final void warn(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { + log(SimpleLog.LOG_LEVEL_WARN, message, t); + } + } + + + /** + *

Log a message with error log level.

+ */ + public final void error(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { + log(SimpleLog.LOG_LEVEL_ERROR, message, null); + } + } + + + /** + *

Log an error with error log level.

+ */ + public final void error(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { + log(SimpleLog.LOG_LEVEL_ERROR, message, t); + } + } + + + /** + *

Log a message with fatal log level.

+ */ + public final void fatal(Object message) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { + log(SimpleLog.LOG_LEVEL_FATAL, message, null); + } + } + + + /** + *

Log an error with fatal log level.

+ */ + public final void fatal(Object message, Throwable t) { + + if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { + log(SimpleLog.LOG_LEVEL_FATAL, message, t); + } + } + + + /** + *

Are debug messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isDebugEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG); + } + + + /** + *

Are error messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isErrorEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR); + } + + + /** + *

Are fatal messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isFatalEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL); + } + + + /** + *

Are info messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isInfoEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO); + } + + + /** + *

Are trace messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isTraceEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE); + } + + + /** + *

Are warn messages currently enabled?

+ * + *

This allows expensive operations such as String + * concatenation to be avoided when the message will be ignored by the + * logger.

+ */ + public final boolean isWarnEnabled() { + + return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN); + } +} +