You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/07/25 17:54:38 UTC

svn commit: r225144 - in /incubator/beehive/trunk/netui/src/util: ./ org/apache/beehive/netui/util/config/ org/apache/beehive/netui/util/logging/ org/apache/beehive/netui/util/logging/internal/

Author: ekoneil
Date: Mon Jul 25 08:54:25 2005
New Revision: 225144

URL: http://svn.apache.org/viewcvs?rev=225144&view=rev
Log:
Add commons-logging abstraction underneath the NetUI Logger.

This allows NetUI to be configured for more than just Log4J logging.  Configuration should be done using the instructions for commons-logging here:

   http://jakarta.apache.org/commons/logging/guide.html

By default, the Log4J logger will still be used.

The original constructors for the o.a.b.n.u.l.Logger class are still around as well and just instantiate the new Log4JLogger class for back-compat.  

One subtle change is that the "commons-logging" appender is no longer removed when using Log4J.  I don't think that this will be an issue, but if anyone has trouble with Struts spewing messages to the console, please file a JIRA issue.

BB: self
DRT: Beehive pass
BVT: NetUI pass



Added:
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/Log4JLogger.java
Modified:
    incubator/beehive/trunk/netui/src/util/build.xml
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/config/ConfigUtil.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/Logger.java

Modified: incubator/beehive/trunk/netui/src/util/build.xml
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/build.xml?rev=225144&r1=225143&r2=225144&view=diff
==============================================================================
--- incubator/beehive/trunk/netui/src/util/build.xml (original)
+++ incubator/beehive/trunk/netui/src/util/build.xml Mon Jul 25 08:54:25 2005
@@ -47,21 +47,6 @@
 
         <!-- copy the default config file used when one isn't found in /WEB-INF/beehive-netui-config.xml -->
         <copy todir="${classes.dir}/${module.name}/org/apache/beehive/netui/util/config" file="${defaultnetuiconfig.xml}"/>
-
-<!--
-        <jar jarfile="${build.lib.dir}/${util.jar.name}" basedir="${classes.dir}/${module.name}">
-            <manifest>
-                <attribute name="Extension-Name" value="Beehive NetUI Util"/>
-                <attribute name="Specification-Title" value="Beehive NetUI Util"/> 
-                <attribute name="Specification-Vendor" value="Apache Software Foundation"/>
-                <attribute name="Specification-Version" value="${beehive.version}"/>
-                <attribute name="Implementation-Title" value="Beehive NetUI Util"/>
-                <attribute name="Implementation-Vendor" value="Apache Software Foundation"/>
-                <attribute name="Implementation-Version" value="${beehive.version}"/>
-                <attribute name="Beehive-Version" value="${beehive.version}"/>
-            </manifest>
-        </jar>
--->
     </target>
 
     <target name="clean">

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/config/ConfigUtil.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/config/ConfigUtil.java?rev=225144&r1=225143&r2=225144&view=diff
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/config/ConfigUtil.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/config/ConfigUtil.java Mon Jul 25 08:54:25 2005
@@ -17,14 +17,13 @@
  */
 package org.apache.beehive.netui.util.config;
 
-import org.apache.beehive.netui.util.internal.InternalStringBuilder;
-
 import java.io.InputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 
 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument;
 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig;
+import org.apache.beehive.netui.util.internal.InternalStringBuilder;
 import org.apache.beehive.netui.util.logging.Logger;
 
 import org.apache.xmlbeans.XmlException;
@@ -85,7 +84,7 @@
     }
     
     public static final boolean isInit() {
-        return (_config != null);
+        return _config != null;
     }
 
     /**
@@ -121,7 +120,7 @@
                 loadOptions.setLoadLineNumbers();
                 _config = NetuiConfigDocument.Factory.parse(is, loadOptions);
             }
-                // XmlException | IOException
+            // XmlException | IOException
             catch(Exception ex) {
                 assert ex instanceof XmlException || ex instanceof IOException;
 

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/Logger.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/Logger.java?rev=225144&r1=225143&r2=225144&view=diff
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/Logger.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/Logger.java Mon Jul 25 08:54:25 2005
@@ -19,149 +19,158 @@
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.lang.reflect.Method;
 
-import org.apache.log4j.Category;
-import org.apache.log4j.Level;
 import org.apache.commons.logging.Log;
 
-public class Logger 
-    implements Log
-{
-    private static final String STRUTS_APPENDER = "commons-logging";
+/**
+ * <p>
+ * Logging abstraction for NetUI.  This class leverages Jakarta commons-logging to create
+ * loggers for NetUI messages.  Application developers can provide their own logger
+ * implementations by following the instructions for creating new Log / LogFactory instances
+ * in commons-logging.
+ * </p>
+ */
+public class Logger
+    implements Log {
 
-    static
-    {
-        // Need to get rid of the appender that Struts adds so
-        // that we don't spam the console with all messages
-        Category root = Category.getRoot();
-
-        if (root.getAppender(STRUTS_APPENDER) != null)
-            root.removeAppender(STRUTS_APPENDER);
+    /**
+     * Factory method for creating NetUI Logger instances.
+     *
+     * @param loggerClient the class whose logger to create
+     * @return a {@link Logger} instance for the given class
+     */
+    public static Logger getInstance(Class loggerClient) {
+        return new Logger(org.apache.commons.logging.LogFactory.getLog(loggerClient.getName()));
     }
 
-    private org.apache.log4j.Logger _logInstance;
+    private Log _logDelegate = null;
 
     /**
-     * Factory method for creating NetUI Logger instances.
+     * Constructor that returns a Log4J logger.  This method is deprecated
+     * in favor of using commons-logging to do logger creation via the
+     * {@link #getInstance(Class)} method.
      *
-     * @param clazz
-     * @return a {@link Logger} instance for the given class
+     * @deprecated
+     * @see #getInstance(Class)
+     * @param clientClass
      */
-    public static Logger getInstance(Class clazz)
-    {
-        return new Logger(clazz);
+    public Logger(Class clientClass) {
+        _logDelegate = createDefaultLogger(clientClass);
     }
 
-    public Logger(Class clazz)
-    {
-        this(clazz.getName());
+    /**
+     * Constructor that returns a Log4J logger.  This method is deprecated
+     * in favor of using commons-logging to do logger creation via the
+     * {@link #getInstance(Class)} method.
+     *
+     * @deprecated
+     * @see #getInstance(Class)
+     * @param clientClassName
+     */
+    public Logger(String clientClassName) {
+        Class clientClass = null;
+        try {
+            /* create a default log4j logger -- this shouldn't throw a CNF exception */
+            clientClass = Class.forName(clientClassName);
+        }
+        catch(ClassNotFoundException e) {
+            throw new IllegalArgumentException("Could not load NetUI logger client class '" + clientClassName + "'");
+        }
+        _logDelegate = createDefaultLogger(clientClass);
     }
 
-    public Logger(String className)
-    {
-        _logInstance = org.apache.log4j.Logger.getLogger(className);
+    /**
+     * Internal method used by the factory to create a Logger instance.
+     *
+     * @param logDelegate the commons-logging {@link Log} to which messages should be logged
+     */
+    private Logger(Log logDelegate) {
+        _logDelegate = logDelegate;
     }
 
-    public boolean isDebugEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.DEBUG);
+    public boolean isDebugEnabled() {
+        return _logDelegate.isDebugEnabled();
     }
 
-    public boolean isErrorEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.ERROR);
+    public boolean isErrorEnabled() {
+        return _logDelegate.isErrorEnabled();
     }
 
-    public boolean isFatalEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.FATAL);
+    public boolean isFatalEnabled() {
+        return _logDelegate.isFatalEnabled();
     }
 
-    public boolean isInfoEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.INFO);
+    public boolean isInfoEnabled() {
+        return _logDelegate.isInfoEnabled();
     }
 
-    public boolean isTraceEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.DEBUG);
+    public boolean isTraceEnabled() {
+        return _logDelegate.isTraceEnabled();
     }
 
-    public boolean isWarnEnabled()
-    {
-        return _logInstance.isEnabledFor(Level.WARN);
+    public boolean isWarnEnabled() {
+        return _logDelegate.isWarnEnabled();
     }
 
-    public void debug(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.DEBUG))
-            _logInstance.debug(message);
+    public void debug(Object message) {
+        if(isDebugEnabled())
+            _logDelegate.debug(message);
     }
 
-    public void debug(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.DEBUG))
-            _logInstance.debug(format(message, t));
+    public void debug(Object message, Throwable t) {
+        if(isDebugEnabled())
+            _logDelegate.debug(format(message, t));
     }
 
-    public void trace(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.DEBUG))
-            _logInstance.debug(message);
+    public void trace(Object message) {
+        if(isTraceEnabled())
+            _logDelegate.trace(message);
     }
 
-    public void trace(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.DEBUG))
-            _logInstance.debug(format(message, t));
+    public void trace(Object message, Throwable t) {
+        if(isTraceEnabled())
+            _logDelegate.trace(format(message, t));
     }
 
-    public void info(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.INFO))
-            _logInstance.info(message);
+    public void info(Object message) {
+        if(isInfoEnabled())
+            _logDelegate.info(message);
     }
 
-    public void info(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.INFO))
-            _logInstance.info(format(message, t));
+    public void info(Object message, Throwable t) {
+        if(isInfoEnabled())
+            _logDelegate.info(format(message, t));
     }
 
-    public void warn(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.WARN))
-            _logInstance.warn(message);
+    public void warn(Object message) {
+        if(isWarnEnabled())
+            _logDelegate.warn(message);
     }
 
-    public void warn(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.WARN))
-            _logInstance.warn(format(message, t));
+    public void warn(Object message, Throwable t) {
+        if(isWarnEnabled())
+            _logDelegate.warn(format(message, t));
     }
 
-    public void error(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.ERROR))
-            _logInstance.error(message);
+    public void error(Object message) {
+        if(isErrorEnabled())
+            _logDelegate.error(message);
     }
 
-    public void error(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.ERROR))
-            _logInstance.error(format(message, t));
+    public void error(Object message, Throwable t) {
+        if(isErrorEnabled())
+            _logDelegate.error(format(message, t));
     }
 
-    public void fatal(Object message)
-    {
-        if (_logInstance.isEnabledFor(Level.FATAL))
-            _logInstance.fatal(message);
+    public void fatal(Object message) {
+        if(isFatalEnabled())
+            _logDelegate.fatal(message);
     }
 
-    public void fatal(Object message, Throwable t)
-    {
-        if (_logInstance.isEnabledFor(Level.FATAL))
-            _logInstance.fatal(format(message, t));
+    public void fatal(Object message, Throwable t) {
+        if(isFatalEnabled())
+            _logDelegate.fatal(format(message, t));
     }
 
     private String format(Object m, Throwable t)
@@ -172,6 +181,31 @@
         StringWriter sw = new StringWriter();
         t.printStackTrace(new PrintWriter(sw));
 
+        /* note, no reason to close a StringWriter */
+
         return m + "\n\n" + "Throwable: " + t.toString() + "\nStack Trace:\n" + sw.toString();
     }
-}
+
+    /**
+     * Internal method used to create the backwards-compat NetUI logger.  This method
+     * looks up the {@link org.apache.beehive.netui.util.logging.internal.Log4JLogger}
+     * and creates a new instance returning the resulting {@link Log}.
+     *
+     * @param loggerClient the logger client
+     * @return the {@link Log} instance
+     */
+    private static final Log createDefaultLogger(Class loggerClient) {
+        assert loggerClient != null : "Received a null loggerClient Class";
+
+        String className = "org.apache.beehive.netui.util.logging.internal.Log4JLogger";
+        try {
+            Class logDelegateClass = Logger.class.getClassLoader().loadClass(className);
+            Method method = logDelegateClass.getMethod("getInstance", new Class[] {Class.class});
+            return (Log)method.invoke(null, new Object[] {loggerClient});
+        }
+        catch(Exception e) {
+            throw new IllegalStateException("Could not create log implementation '" + className +
+                "' for client of type '" + loggerClient.getName() + "'", e);
+        }
+    }
+}
\ No newline at end of file

Added: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/Log4JLogger.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/Log4JLogger.java?rev=225144&view=auto
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/Log4JLogger.java (added)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/logging/internal/Log4JLogger.java Mon Jul 25 08:54:25 2005
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.logging.internal;
+
+import java.io.StringWriter;
+import java.io.PrintWriter;
+
+import org.apache.commons.logging.Log;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.Category;
+
+/**
+ * <p>
+ * Logging abstraction used to pipe log messages to Log4J.  This class is used for
+ * NetUI backwards compatability so that previous {@link org.apache.beehive.netui.util.logging.Logger}
+ * clients continue to log through the usual Log4J channels.
+ * </p>
+ *
+ * @deprecated
+ */
+public final class Log4JLogger
+    implements Log {
+
+    private static final String STRUTS_APPENDER = "commons-logging";
+
+    static {
+        // Need to get rid of the appender that Struts adds so
+        // that we don't spam the console with all messages
+        Category root = Category.getRoot();
+
+        if (root.getAppender(STRUTS_APPENDER) != null)
+            root.removeAppender(STRUTS_APPENDER);
+    }
+
+    private Logger _logInstance;
+
+    public static Log getInstance(Class clazz) {
+        return new Log4JLogger(clazz);
+    }
+
+    private Log4JLogger(Class clazz) {
+        this(clazz.getName());
+    }
+
+    private Log4JLogger(String className) {
+        _logInstance = Logger.getLogger(className);
+    }
+
+    public boolean isDebugEnabled() {
+        return _logInstance.isEnabledFor(Level.DEBUG);
+    }
+
+    public boolean isErrorEnabled() {
+        return _logInstance.isEnabledFor(Level.ERROR);
+    }
+
+    public boolean isFatalEnabled() {
+        return _logInstance.isEnabledFor(Level.FATAL);
+    }
+
+    public boolean isInfoEnabled() {
+        return _logInstance.isEnabledFor(Level.INFO);
+    }
+
+    public boolean isTraceEnabled() {
+        return _logInstance.isEnabledFor(Level.DEBUG);
+    }
+
+    public boolean isWarnEnabled() {
+        return _logInstance.isEnabledFor(Level.WARN);
+    }
+
+    public void debug(Object message) {
+        if (_logInstance.isEnabledFor(Level.DEBUG))
+            _logInstance.debug(message);
+    }
+
+    public void debug(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.DEBUG))
+            _logInstance.debug(format(message, t));
+    }
+
+    public void trace(Object message) {
+        if (_logInstance.isEnabledFor(Level.DEBUG))
+            _logInstance.debug(message);
+    }
+
+    public void trace(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.DEBUG))
+            _logInstance.debug(format(message, t));
+    }
+
+    public void info(Object message) {
+        if (_logInstance.isEnabledFor(Level.INFO))
+            _logInstance.info(message);
+    }
+
+    public void info(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.INFO))
+            _logInstance.info(format(message, t));
+    }
+
+    public void warn(Object message) {
+        if (_logInstance.isEnabledFor(Level.WARN))
+            _logInstance.warn(message);
+    }
+
+    public void warn(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.WARN))
+            _logInstance.warn(format(message, t));
+    }
+
+    public void error(Object message) {
+        if (_logInstance.isEnabledFor(Level.ERROR))
+            _logInstance.error(message);
+    }
+
+    public void error(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.ERROR))
+            _logInstance.error(format(message, t));
+    }
+
+    public void fatal(Object message) {
+        if (_logInstance.isEnabledFor(Level.FATAL))
+            _logInstance.fatal(message);
+    }
+
+    public void fatal(Object message, Throwable t) {
+        if (_logInstance.isEnabledFor(Level.FATAL))
+            _logInstance.fatal(format(message, t));
+    }
+
+    private String format(Object m, Throwable t) {
+        if(t == null)
+            return m.toString();
+
+        StringWriter sw = new StringWriter();
+        t.printStackTrace(new PrintWriter(sw));
+
+        return m + "\n\n" + "Throwable: " + t.toString() + "\nStack Trace:\n" + sw.toString();
+    }
+}