craigmcc 02/01/05 14:40:40 Modified: logging build.xml logging/src/java/org/apache/commons/logging LogSource.java package.html Added: logging/src/java/org/apache/commons/logging Jdk14Logger.java Log: Add a new Log implementation for JDK 1.4 (or later) logging. Default behavior of LogSource.getInstance() is now: * If Log4J is available, return an instance of Log4JCategoryLog * If JDK 1.4 is available, return an instance of Jdk14Logger * Otherwise, return an instance of NoOpLogger As before, this default behavior can be overridden with a system property, or by calling LogSource.setLogImplementation(), as described in the package Javadocs. Revision Changes Path 1.5 +6 -1 jakarta-commons/logging/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/jakarta-commons/logging/build.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- build.xml 5 Jan 2002 00:34:46 -0000 1.4 +++ build.xml 5 Jan 2002 22:40:40 -0000 1.5 @@ -3,7 +3,7 @@ <!-- "Logging" component of the Jakarta Commons Subproject - $Id: build.xml,v 1.4 2002/01/05 00:34:46 craigmcc Exp $ + $Id: build.xml,v 1.5 2002/01/05 22:40:40 craigmcc Exp $ --> @@ -129,12 +129,17 @@ <target name="compile" depends="static" description="Compile shareable components"> + <available property="jdk.1.4.present" + classname="java.util.logging.Logger"/> + <echo message="jdk.1.4.present=${jdk.1.4.present}"/> <javac srcdir="${source.home}" destdir="${build.home}/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"> <classpath refid="compile.classpath"/> + <exclude name="org/apache/commons/logging/Jdk14Logger.java" + unless="jdk.1.4.present"/> </javac> <copy todir="${build.home}/classes" filtering="on"> <fileset dir="${source.home}" excludes="**/*.java"/> 1.7 +102 -47 jakarta-commons/logging/src/java/org/apache/commons/logging/LogSource.java Index: LogSource.java =================================================================== RCS file: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/LogSource.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- LogSource.java 5 Jan 2002 15:55:00 -0000 1.6 +++ LogSource.java 5 Jan 2002 22:40:40 -0000 1.7 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/LogSource.java,v 1.6 2002/01/05 15:55:00 rdonkin Exp $ - * $Revision: 1.6 $ - * $Date: 2002/01/05 15:55:00 $ + * $Header: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/LogSource.java,v 1.7 2002/01/05 22:40:40 craigmcc Exp $ + * $Revision: 1.7 $ + * $Date: 2002/01/05 22:40:40 $ * * ==================================================================== * @@ -68,66 +68,123 @@ /** * <p>Factory for creating {@link Log} instances. Applications should call - * the {@link #makeNewLogInstance} method to instantiate new instances + * the <code>makeNewLogInstance()</code> method to instantiate new instances * of the configured {@link Log} implementation class.</p> * + * <p>By default, calling <code>getInstance()</code> will use the following + * algorithm:</p> + * <ul> + * <li>If Log4J is available, return an instance of + * <code>org.apache.commons.logging.Log4JCategoryLog</code>.</li> + * <li>If JDK 1.4 or later is available, return an instance of + * <code>org.apache.commons.logging.Jdk14Logger</code>.</li> + * <li>Otherwise, return an instance of + * <code>org.apache.commons.logging.NoOpLog</code>.</li> + * </ul> + * + * <p>You can change the default behavior in one of two ways:</p> + * <ul> + * <li>On the startup command line, set the system property + * <code>org.apache.commons.logging.log</code> to the name of the + * <code>org.apache.commons.logging.Log</code> implementation class + * you want to use.</li> + * <li>At runtime, call <code>LogSource.setLogImplementation()</code>.</li> + * </ul> + * * @author Rod Waldhoff - * @version $Id: LogSource.java,v 1.6 2002/01/05 15:55:00 rdonkin Exp $ + * @version $Id: LogSource.java,v 1.7 2002/01/05 22:40:40 craigmcc Exp $ */ public class LogSource { - // --------------------------------------------------------- Class Attributes + // ------------------------------------------------------- Class Attributes static protected HashMap _logs = new HashMap(); + /** Is log4j available (in the current classpath) */ static protected boolean _log4jIsAvailable = false; + + /** Is JD 1.4 logging available */ + static protected boolean _jdk14IsAvailable = false; - - // --------------------------------------------------------- Class Initializers + /** Constructor for current log class */ + static protected Constructor _logimplctor = null; + + + // ----------------------------------------------------- Class Initializers static { + + // Is Log4J Available? try { if(null != Class.forName("org.apache.log4j.Category")) { _log4jIsAvailable = true; } else { _log4jIsAvailable = false; } - } catch(ClassNotFoundException e) { - _log4jIsAvailable = false; - } catch(ExceptionInInitializerError e) { - _log4jIsAvailable = false; - } catch(LinkageError e) { + } catch (Throwable t) { _log4jIsAvailable = false; } - } - /** Constructor for current log class */ - static protected Constructor _logimplctor = null; - static { + // Is JDK 1.4 Logging Available? try { - setLogImplementation( - System.getProperty( - "org.apache.commons.logging.log","org.apache.commons.logging.NoOpLog")); - - } catch(SecurityException e) { - _logimplctor = null; - } catch(LinkageError e) { - _logimplctor = null; - } catch(NoSuchMethodException e) { - _logimplctor = null; - } catch(ClassNotFoundException e) { - _logimplctor = null; + if(null != Class.forName("java.util.logging.Logger")) { + _jdk14IsAvailable = true; + } else { + _jdk14IsAvailable = false; + } + } catch (Throwable t) { + _jdk14IsAvailable = false; + } + + // Set the default Log implementation + String name = + System.getProperty("org.apache.commons.logging.log"); + if (name != null) { + try { + setLogImplementation(name); + } catch (Throwable t) { + try { + setLogImplementation + ("org.apache.commons.logging.NoOpLog"); + } catch (Throwable u) { + ; + } + } + } else { + try { + if (_log4jIsAvailable) { + setLogImplementation + ("org.apache.commons.logging.Log4JCategoryLog"); + } else if (_jdk14IsAvailable) { + setLogImplementation + ("org.apache.commons.logging.Jdk14Logger"); + } else { + setLogImplementation + ("org.apache.commons.logging.NoOpLog"); + } + } catch (Throwable t) { + try { + setLogImplementation + ("org.apache.commons.logging.NoOpLog"); + } catch (Throwable u) { + ; + } + } } + } - // --------------------------------------------------------- Constructor + // ------------------------------------------------------------ Constructor + /** Don't allow others to create instances */ private LogSource() { } - // --------------------------------------------------------- Class Methods + + // ---------------------------------------------------------- Class Methods + /** * Set the log implementation/log implementation factory @@ -140,12 +197,17 @@ LinkageError, ExceptionInInitializerError, NoSuchMethodException, SecurityException, ClassNotFoundException { - Class logclass = Class.forName(classname); - Class[] argtypes = new Class[1]; - argtypes[0] = "".getClass(); - _logimplctor = logclass.getConstructor(argtypes); + try { + Class logclass = Class.forName(classname); + Class[] argtypes = new Class[1]; + argtypes[0] = "".getClass(); + _logimplctor = logclass.getConstructor(argtypes); + } catch (Throwable t) { + _logimplctor = null; + } } + /** * Set the log implementation/log implementation factory * by class. The given class must implement {@link Log}, @@ -171,11 +233,13 @@ return log; } + /** Get a <code>Log</code> instance by class */ static public Log getInstance(Class clazz) { return getInstance(clazz.getName()); } + /** * Create a new {@link Log} implementation, based * on the given <i>name</i> @@ -194,36 +258,27 @@ * or when no corresponding class can be found, * this method will return a {@link Log4JCategoryLog} * if the log4j {@link org.apache.log4j.Category} class is - * available in the {@link LogSource}'s classpath, or - * a {@link NoOpLog} if it is not. + * available in the {@link LogSource}'s classpath, or a + * {@link Jdk14Logger} if we are on a JDK 1.4 or later system, or + * a {@link NoOpLog} if neither of the above conditions is true. * * @param name the log name (or category) */ static public Log makeNewLogInstance(String name) { + Log log = null; try { Object[] args = new Object[1]; args[0] = name; log = (Log)(_logimplctor.newInstance(args)); - } catch (InstantiationException e) { - log = null; - } catch (IllegalAccessException e) { - log = null; - } catch (IllegalArgumentException e) { - log = null; - } catch (InvocationTargetException e) { - log = null; - } catch (NullPointerException e) { + } catch (Throwable t) { log = null; } if(null == log) { - if(_log4jIsAvailable) { - return new Log4JCategoryLog(name); - } else { - log = new NoOpLog(name); - } + log = new NoOpLog(name); } return log; + } /** 1.2 +4 -8 jakarta-commons/logging/src/java/org/apache/commons/logging/package.html Index: package.html =================================================================== RCS file: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/package.html,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- package.html 4 Dec 2001 04:28:03 -0000 1.1 +++ package.html 5 Jan 2002 22:40:40 -0000 1.2 @@ -10,12 +10,10 @@ <li><a href="http://jakarta.apache.org/log4j/">Log4J</a> from Apache's Jakarta project. Each named <a href="Log.html">Log</a> instance is connected to a corresponding Log4J Category.</li> -<!-- <li><a href="http://java.sun.com/j2se/1.4/docs/guide/util/logging/index.html"> JDK Logging API</a>, included in JDK 1.4 or later systems. Each named <a href="Log.html">Log</a> instance is connected to a corresponding <code>java.util.logging.Logger</code> instance.</li> ---> <li><a href="NoOpLog.html">NoOpLog</a> implementation that simply swallows all log output, for all named <a href="Log.html">Log</a> isntances.</li> <li><a href="SimpleLog.html">SimpleLog</a> implementation that writes all @@ -41,11 +39,9 @@ <li>If Log4J is available, return an instance of <a href="Log4JCategoryLog.html">Log4JCategoryLog</a> that wraps a Log4J Category instance of the specified name.</li> -<!-- <li>If the JDK 1.4 logging APIs are available, return an instance - of <a href="JdkLogger.html">JdkLogger</a> that wraps an instance of + of <a href="Jdk14Logger.html">Jdk14Logger</a> that wraps an instance of <code>java.util.logging.Logger</code> for the specified name.</li> ---> <li>Return an instance of <a href="NoOpLog.html">NoOpLog</a> that throws away all logged output.</li> </ul> @@ -68,8 +64,8 @@ <li>Acquire a reference to an instance of <a href="Log.html">org.apache.commons.logging.Log</a>, by calling the factory method - <a href="LogSource.html#makeNewLogInstance(java.lang.String)"> - LogSource.makeNewLogInstance()</a>. Your application can contain + <a href="LogSource.html#getInstance(java.lang.String)"> + LogSource.getInstance(String name)</a>. Your application can contain references to multiple loggers that are used for different purposes. A typical scenario for a server application is to have each major component of the server use its own Log instance.</li> @@ -91,7 +87,7 @@ public class MyComponent { - protected Log log = LogSource.makeNewInstance("mycomponent"); + protected Log log = LogSource.getInstance("my.component"); // Called once at startup time public void start() { 1.1 jakarta-commons/logging/src/java/org/apache/commons/logging/Jdk14Logger.java Index: Jdk14Logger.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/Jdk14Logger.java,v 1.1 2002/01/05 22:40:40 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/01/05 22:40:40 $ * * ==================================================================== * * 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 [EMAIL PROTECTED] * * 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.commons.logging; import java.util.logging.Level; import java.util.logging.Logger; /** * <p>Implementation of the <code>org.apache.commons.logging.Log</code> * interfaces that wraps the standard JDK logging mechanisms that were * introduced in the Merlin release (JDK 1.4).</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/01/05 22:40:40 $ */ public 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.FINEST, message.toString()); } /** * Log a message and exception with debug log level. */ public void debug(Object message, Throwable exception) { logger.log(Level.FINEST, 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 current logging level. */ public int getLevel() { Level level = logger.getLevel(); if (level == Level.ALL) { return (ALL); } else if (level == Level.SEVERE) { return (ERROR); } else if (level == Level.WARNING) { return (WARN); } else if (level == Level.INFO) { return (INFO); } else if (level == Level.CONFIG) { return (DEBUG); } else if (level == Level.FINE) { return (DEBUG); } else if (level == Level.FINER) { return (DEBUG); } else if (level == Level.FINEST) { return (DEBUG); } else { return (OFF); } } /** * 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.FINEST)); } /** * Is info logging currently enabled? */ public boolean isInfoEnabled() { return (logger.isLoggable(Level.INFO)); } /** * Set the new logging level. * * @param level New logging level */ public void setLevel(int level) { if (level == OFF) { logger.setLevel(Level.OFF); } else if (level >= FATAL) { logger.setLevel(Level.SEVERE); } else if (level >= ERROR) { logger.setLevel(Level.SEVERE); } else if (level >= WARN) { logger.setLevel(Level.WARNING); } else if (level >= INFO) { logger.setLevel(Level.INFO); } else if (level >= DEBUG) { logger.setLevel(Level.FINEST); } else if (level >= ALL) { logger.setLevel(Level.ALL); } } /** * 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); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>