1
0

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.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138829 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Craig R. McClanahan
2002-01-05 22:40:40 +00:00
parent 09277f80cb
commit af62db1c56
4 changed files with 412 additions and 60 deletions

View File

@@ -3,7 +3,7 @@
<!-- <!--
"Logging" component of the Jakarta Commons Subproject "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" <target name="compile" depends="static"
description="Compile shareable components"> 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}" <javac srcdir="${source.home}"
destdir="${build.home}/classes" destdir="${build.home}/classes"
debug="${compile.debug}" debug="${compile.debug}"
deprecation="${compile.deprecation}" deprecation="${compile.deprecation}"
optimize="${compile.optimize}"> optimize="${compile.optimize}">
<classpath refid="compile.classpath"/> <classpath refid="compile.classpath"/>
<exclude name="org/apache/commons/logging/Jdk14Logger.java"
unless="jdk.1.4.present"/>
</javac> </javac>
<copy todir="${build.home}/classes" filtering="on"> <copy todir="${build.home}/classes" filtering="on">
<fileset dir="${source.home}" excludes="**/*.java"/> <fileset dir="${source.home}" excludes="**/*.java"/>

View File

@@ -0,0 +1,296 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/Attic/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 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.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);
}
}

View File

@@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/LogSource.java,v 1.6 2002/01/05 15:55:00 rdonkin Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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.6 $ * $Revision: 1.7 $
* $Date: 2002/01/05 15:55:00 $ * $Date: 2002/01/05 22:40:40 $
* *
* ==================================================================== * ====================================================================
* *
@@ -68,66 +68,123 @@ import java.lang.reflect.InvocationTargetException;
/** /**
* <p>Factory for creating {@link Log} instances. Applications should call * <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> * 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 * @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 { public class LogSource {
// --------------------------------------------------------- Class Attributes // ------------------------------------------------------- Class Attributes
static protected HashMap _logs = new HashMap(); static protected HashMap _logs = new HashMap();
/** Is log4j available (in the current classpath) */ /** Is log4j available (in the current classpath) */
static protected boolean _log4jIsAvailable = false; static protected boolean _log4jIsAvailable = false;
/** Is JD 1.4 logging available */
static protected boolean _jdk14IsAvailable = false;
/** Constructor for current log class */
// --------------------------------------------------------- Class Initializers static protected Constructor _logimplctor = null;
// ----------------------------------------------------- Class Initializers
static { static {
// Is Log4J Available?
try { try {
if(null != Class.forName("org.apache.log4j.Category")) { if(null != Class.forName("org.apache.log4j.Category")) {
_log4jIsAvailable = true; _log4jIsAvailable = true;
} else { } else {
_log4jIsAvailable = false; _log4jIsAvailable = false;
} }
} catch(ClassNotFoundException e) { } catch (Throwable t) {
_log4jIsAvailable = false;
} catch(ExceptionInInitializerError e) {
_log4jIsAvailable = false;
} catch(LinkageError e) {
_log4jIsAvailable = false; _log4jIsAvailable = false;
} }
}
/** Constructor for current log class */ // Is JDK 1.4 Logging Available?
static protected Constructor _logimplctor = null;
static {
try { try {
setLogImplementation( if(null != Class.forName("java.util.logging.Logger")) {
System.getProperty( _jdk14IsAvailable = true;
"org.apache.commons.logging.log","org.apache.commons.logging.NoOpLog")); } else {
_jdk14IsAvailable = false;
} catch(SecurityException e) { }
_logimplctor = null; } catch (Throwable t) {
} catch(LinkageError e) { _jdk14IsAvailable = false;
_logimplctor = null;
} catch(NoSuchMethodException e) {
_logimplctor = null;
} catch(ClassNotFoundException e) {
_logimplctor = null;
} }
// 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 */ /** Don't allow others to create instances */
private LogSource() { private LogSource() {
} }
// --------------------------------------------------------- Class Methods
// ---------------------------------------------------------- Class Methods
/** /**
* Set the log implementation/log implementation factory * Set the log implementation/log implementation factory
@@ -140,12 +197,17 @@ public class LogSource {
LinkageError, ExceptionInInitializerError, LinkageError, ExceptionInInitializerError,
NoSuchMethodException, SecurityException, NoSuchMethodException, SecurityException,
ClassNotFoundException { ClassNotFoundException {
Class logclass = Class.forName(classname); try {
Class[] argtypes = new Class[1]; Class logclass = Class.forName(classname);
argtypes[0] = "".getClass(); Class[] argtypes = new Class[1];
_logimplctor = logclass.getConstructor(argtypes); argtypes[0] = "".getClass();
_logimplctor = logclass.getConstructor(argtypes);
} catch (Throwable t) {
_logimplctor = null;
}
} }
/** /**
* Set the log implementation/log implementation factory * Set the log implementation/log implementation factory
* by class. The given class must implement {@link Log}, * by class. The given class must implement {@link Log},
@@ -171,11 +233,13 @@ public class LogSource {
return log; return log;
} }
/** Get a <code>Log</code> instance by class */ /** Get a <code>Log</code> instance by class */
static public Log getInstance(Class clazz) { static public Log getInstance(Class clazz) {
return getInstance(clazz.getName()); return getInstance(clazz.getName());
} }
/** /**
* Create a new {@link Log} implementation, based * Create a new {@link Log} implementation, based
* on the given <i>name</i> * on the given <i>name</i>
@@ -194,36 +258,27 @@ public class LogSource {
* or when no corresponding class can be found, * or when no corresponding class can be found,
* this method will return a {@link Log4JCategoryLog} * this method will return a {@link Log4JCategoryLog}
* if the log4j {@link org.apache.log4j.Category} class is * if the log4j {@link org.apache.log4j.Category} class is
* available in the {@link LogSource}'s classpath, or * available in the {@link LogSource}'s classpath, or a
* a {@link NoOpLog} if it is not. * {@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) * @param name the log name (or category)
*/ */
static public Log makeNewLogInstance(String name) { static public Log makeNewLogInstance(String name) {
Log log = null; Log log = null;
try { try {
Object[] args = new Object[1]; Object[] args = new Object[1];
args[0] = name; args[0] = name;
log = (Log)(_logimplctor.newInstance(args)); log = (Log)(_logimplctor.newInstance(args));
} catch (InstantiationException e) { } catch (Throwable t) {
log = null;
} catch (IllegalAccessException e) {
log = null;
} catch (IllegalArgumentException e) {
log = null;
} catch (InvocationTargetException e) {
log = null;
} catch (NullPointerException e) {
log = null; log = null;
} }
if(null == log) { if(null == log) {
if(_log4jIsAvailable) { log = new NoOpLog(name);
return new Log4JCategoryLog(name);
} else {
log = new NoOpLog(name);
}
} }
return log; return log;
} }
/** /**

View File

@@ -10,12 +10,10 @@ prebuilt support for the following:</p>
<li><a href="http://jakarta.apache.org/log4j/">Log4J</a> from Apache's <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 Jakarta project. Each named <a href="Log.html">Log</a> instance is
connected to a corresponding Log4J Category.</li> connected to a corresponding Log4J Category.</li>
<!--
<li><a href="http://java.sun.com/j2se/1.4/docs/guide/util/logging/index.html"> <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 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 <a href="Log.html">Log</a> instance is connected to a corresponding
<code>java.util.logging.Logger</code> instance.</li> <code>java.util.logging.Logger</code> instance.</li>
-->
<li><a href="NoOpLog.html">NoOpLog</a> implementation that simply swallows <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> 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 <li><a href="SimpleLog.html">SimpleLog</a> implementation that writes all
@@ -41,11 +39,9 @@ following algorithm is applied:</p>
<li>If Log4J is available, return an instance of <li>If Log4J is available, return an instance of
<a href="Log4JCategoryLog.html">Log4JCategoryLog</a> that wraps a <a href="Log4JCategoryLog.html">Log4JCategoryLog</a> that wraps a
Log4J Category instance of the specified name.</li> Log4J Category instance of the specified name.</li>
<!--
<li>If the JDK 1.4 logging APIs are available, return an instance <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> <code>java.util.logging.Logger</code> for the specified name.</li>
-->
<li>Return an instance of <a href="NoOpLog.html">NoOpLog</a> that <li>Return an instance of <a href="NoOpLog.html">NoOpLog</a> that
throws away all logged output.</li> throws away all logged output.</li>
</ul> </ul>
@@ -68,8 +64,8 @@ component, consists of the following steps:</p>
<li>Acquire a reference to an instance of <li>Acquire a reference to an instance of
<a href="Log.html">org.apache.commons.logging.Log</a>, by calling the <a href="Log.html">org.apache.commons.logging.Log</a>, by calling the
factory method factory method
<a href="LogSource.html#makeNewLogInstance(java.lang.String)"> <a href="LogSource.html#getInstance(java.lang.String)">
LogSource.makeNewLogInstance()</a>. Your application can contain LogSource.getInstance(String name)</a>. Your application can contain
references to multiple loggers that are used for different references to multiple loggers that are used for different
purposes. A typical scenario for a server application is to have each purposes. A typical scenario for a server application is to have each
major component of the server use its own Log instance.</li> major component of the server use its own Log instance.</li>
@@ -91,7 +87,7 @@ import org.apache.commons.logging.LogSource;
public class MyComponent { public class MyComponent {
protected Log log = LogSource.makeNewInstance("mycomponent"); protected Log log = LogSource.getInstance("my.component");
// Called once at startup time // Called once at startup time
public void start() { public void start() {