From af62db1c566366526d8ed0e4ba699f3e10252f57 Mon Sep 17 00:00:00 2001
From: "Craig R. McClanahan"
Date: Sat, 5 Jan 2002 22:40:40 +0000
Subject: [PATCH] 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
---
build.xml | 7 +-
.../apache/commons/logging/Jdk14Logger.java | 296 ++++++++++++++++++
.../org/apache/commons/logging/LogSource.java | 157 +++++++---
.../org/apache/commons/logging/package.html | 12 +-
4 files changed, 412 insertions(+), 60 deletions(-)
create mode 100644 src/java/org/apache/commons/logging/Jdk14Logger.java
diff --git a/build.xml b/build.xml
index 7efe7b2..c868222 100644
--- a/build.xml
+++ b/build.xml
@@ -3,7 +3,7 @@
@@ -129,12 +129,17 @@
+
+
+
diff --git a/src/java/org/apache/commons/logging/Jdk14Logger.java b/src/java/org/apache/commons/logging/Jdk14Logger.java
new file mode 100644
index 0000000..5a5536e
--- /dev/null
+++ b/src/java/org/apache/commons/logging/Jdk14Logger.java
@@ -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
+ * .
+ *
+ */
+
+
+package org.apache.commons.logging;
+
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+
+/**
+ * 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 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);
+
+ }
+
+
+}
diff --git a/src/java/org/apache/commons/logging/LogSource.java b/src/java/org/apache/commons/logging/LogSource.java
index eb4be61..4ea78df 100644
--- a/src/java/org/apache/commons/logging/LogSource.java
+++ b/src/java/org/apache/commons/logging/LogSource.java
@@ -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 $
- * $Revision: 1.6 $
- * $Date: 2002/01/05 15:55:00 $
+ * $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.7 $
+ * $Date: 2002/01/05 22:40:40 $
*
* ====================================================================
*
@@ -68,66 +68,123 @@ import java.lang.reflect.InvocationTargetException;
/**
* Factory for creating {@link Log} instances. Applications should call
- * the {@link #makeNewLogInstance} method to instantiate new instances
+ * the makeNewLogInstance() method to instantiate new instances
* of the configured {@link Log} implementation class.
*
+ * By default, calling getInstance() will use the following
+ * algorithm:
+ *
+ * - If Log4J is available, return an instance of
+ *
org.apache.commons.logging.Log4JCategoryLog.
+ * - If JDK 1.4 or later is available, return an instance of
+ *
org.apache.commons.logging.Jdk14Logger.
+ * - Otherwise, return an instance of
+ *
org.apache.commons.logging.NoOpLog.
+ *
+ *
+ * You can change the default behavior in one of two ways:
+ *
+ * - On the startup command line, set the system property
+ *
org.apache.commons.logging.log to the name of the
+ * org.apache.commons.logging.Log implementation class
+ * you want to use.
+ * - At runtime, call
LogSource.setLogImplementation().
+ *
+ *
* @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 @@ public class LogSource {
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 @@ public class LogSource {
return log;
}
+
/** Get a Log instance by class */
static public Log getInstance(Class clazz) {
return getInstance(clazz.getName());
}
+
/**
* Create a new {@link Log} implementation, based
* on the given name
@@ -194,36 +258,27 @@ public class LogSource {
* 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;
+
}
/**
diff --git a/src/java/org/apache/commons/logging/package.html b/src/java/org/apache/commons/logging/package.html
index 123daba..21cec49 100644
--- a/src/java/org/apache/commons/logging/package.html
+++ b/src/java/org/apache/commons/logging/package.html
@@ -10,12 +10,10 @@ prebuilt support for the following:
Log4J from Apache's
Jakarta project. Each named Log instance is
connected to a corresponding Log4J Category.
-
NoOpLog implementation that simply swallows
all log output, for all named Log isntances.
SimpleLog implementation that writes all
@@ -41,11 +39,9 @@ following algorithm is applied:
If Log4J is available, return an instance of
Log4JCategoryLog that wraps a
Log4J Category instance of the specified name.
-
Return an instance of NoOpLog that
throws away all logged output.
@@ -68,8 +64,8 @@ component, consists of the following steps:
Acquire a reference to an instance of
org.apache.commons.logging.Log, by calling the
factory method
-
- LogSource.makeNewLogInstance(). Your application can contain
+
+ LogSource.getInstance(String name). 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.
@@ -91,7 +87,7 @@ import org.apache.commons.logging.LogSource;
public class MyComponent {
- protected Log log = LogSource.makeNewInstance("mycomponent");
+ protected Log log = LogSource.getInstance("my.component");
// Called once at startup time
public void start() {