1
0

Removed AbstractLog and associated test case, removed log level constants from Log interface, added renamed level constants to SimpleLog and fixed SimpleLog configuration bug

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138836 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Burrell Donkin
2002-01-17 22:55:43 +00:00
parent b39b2191aa
commit 7d84a88865
7 changed files with 288 additions and 1069 deletions

View File

@@ -1,500 +0,0 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/Attic/AbstractLog.java,v 1.2 2002/01/17 01:47:49 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2002/01/17 01:47:49 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.logging;
/**
* <p> This is an abstract implementation of the <code>Log</code> interface.
* It provides the following common services for the actual concrete
* implementations:
*
* <h4> Log Level Property </h4>
* <p> Property getter and setter for log levels. </p>
*
* <h4> Log Level Enforcement</h4>
* <p> The current log level is checked and then the message will be passed
* onto the subclass implementation if the level is currently enabled. </p>
*
* @author Robert Burrell Donkin
* @version $Revision: 1.2 $
*/
public abstract class AbstractLog implements Log {
// ------------------------------------------------------------- Attributes
/** Default log level is currently <code>OFF</code> */
private int currentLogLevel = Log.OFF;
// ------------------------------------------------------------- Properties
/**
* <p> Set logging level. </p>
*
* @param level new logging level
*/
public void setLevel(int currentLogLevel) {
this.currentLogLevel = currentLogLevel;
}
/**
* <p> Get logging level. </p>
*/
public int getLevel() {
return currentLogLevel;
}
// -------------------------------------------------------- Logging Methods
/**
* <p> Log a message with debug log level.</p>
*
* <p> If debug log level is enabled,
* then {@link #debugImpl(Object message)} is called. </p>
*/
public final void debug(Object message) {
if (isLevelEnabled(Log.DEBUG)) {
debugImpl(message);
}
}
/**
* <p> Log an error with debug log level.</p>
*
* <p> If debug log level is enabled,
* then {@link #debugImpl(Object message, Throwable t)} is called. </p>
*/
public final void debug(Object message, Throwable t) {
if (isLevelEnabled(Log.DEBUG)) {
debugImpl(message, t);
}
}
/**
* <p> Log a message with info log level.</p>
*
* <p> If info log level is enabled,
* then {@link #infoImpl(Object message)} is called. </p>
*/
public final void info(Object message) {
if (isLevelEnabled(Log.INFO)) {
infoImpl(message);
}
}
/**
* <p> Log an error with info log level.</p>
*
* <p> If info log level is enabled,
* then {@link #infoImpl(Object message, Throwable t)} is called. </p>
*/
public final void info(Object message, Throwable t) {
if (isLevelEnabled(Log.INFO)) {
infoImpl(message, t);
}
}
/**
* <p> Log a message with warn log level.</p>
*
* <p> If warn log level is enabled,
* then {@link #warnImpl(Object message)} is called. </p>
*/
public final void warn(Object message) {
if (isLevelEnabled(Log.WARN)) {
warnImpl(message);
}
}
/**
* <p> Log an error with warn log level.</p>
*
* <p> If warn log level is enabled,
* then {@link #warnImpl(Object message, Throwable t)} is called. </p>
*/
public final void warn(Object message, Throwable t) {
if (isLevelEnabled(Log.WARN)) {
warnImpl(message, t);
}
}
/**
* <p> Log a message with error log level.</p>
*
* <p> If error log level is enabled,
* then {@link #errorImpl(Object message)} is called. </p>
*/
public final void error(Object message) {
if (isLevelEnabled(Log.ERROR)) {
errorImpl(message);
}
}
/**
* <p> Log an error with error log level.</p>
*
* <p> If error log level is enabled,
* then {@link #errorImpl(Object message, Throwable t)} is called. </p>
*/
public final void error(Object message, Throwable t) {
if (isLevelEnabled(Log.ERROR)) {
errorImpl(message, t);
}
}
/**
* <p> Log a message with fatal log level.</p>
*
* <p> If fatal log level is enabled,
* then {@link #fatalImpl(Object message)} is called. </p>
*/
public final void fatal(Object message) {
if (isLevelEnabled(Log.FATAL)) {
fatalImpl(message);
}
}
/**
* <p> Log an error with fatal log level.</p>
*
* <p> If fatal log level is enabled,
* then {@link #fatalImpl(Object message, Throwable t)} is called. </p>
*/
public final void fatal(Object message, Throwable t) {
if (isLevelEnabled(Log.FATAL)) {
fatalImpl(message, t);
}
}
/**
* <p> Are debug messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*
* <p> This implementation checks that the log level is debug or lower.
* If it is, then it passes the request onto {@link #isDebugEnabledImpl}.
*/
public final boolean isDebugEnabled() {
if (isLevelEnabled(Log.DEBUG)) {
return isDebugEnabledImpl();
}
return false;
}
/**
* <p> Are error messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*
* <p> This implementation checks that the log level is error or lower.
* If it is, then it passes the request onto {@link #isErrorEnabledImpl}.
*/
public final boolean isErrorEnabled() {
if (isLevelEnabled(Log.ERROR)) {
return isErrorEnabledImpl();
}
return false;
}
/**
* <p> Are fatal messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*
* <p> This implementation checks that the log level is fatal or lower.
* If it is, then it passes the request onto {@link #isFatalEnabledImpl}.
*/
public final boolean isFatalEnabled() {
if (isLevelEnabled(Log.FATAL)) {
return isFatalEnabledImpl();
}
return false;
}
/**
* <p> Are info messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*
* <p> This implementation checks that the log level is info or lower.
* If it is, then it passes the request onto {@link #isInfoEnabledImpl}.
*/
public final boolean isInfoEnabled() {
if (isLevelEnabled(Log.INFO)) {
return isInfoEnabledImpl();
}
return false;
}
/**
* <p> Are warn messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*
* <p> This implementation checks that the log level is warn or lower.
* If it is, then it passes the request onto {@link #isWarnEnabledImpl}.
*/
public final boolean isWarnEnabled() {
if (isLevelEnabled(Log.WARN)) {
return isWarnEnabledImpl();
}
return false;
}
// ----------------------------------------------- Decorated Implementation
/**
* <p> [OVERRIDE] Log a message with debug log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void debugImpl(Object message);
/**
* <p> [OVERRIDE] Log an error with debug log level.
* Subclasses should override this method to implement the logging. </p>
*/
protected abstract void debugImpl(Object message, Throwable t);
/**
* <p> [OVERRIDE] Log a message with info log level .
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void infoImpl(Object message);
/**
* <p> [OVERRIDE] Log an error with info log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void infoImpl(Object message, Throwable t);
/**
* <p> [OVERRIDE] Log a message with warn log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void warnImpl(Object message);
/**
* <p> [OVERRIDE] Log an error with warn log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void warnImpl(Object message, Throwable t);
/**
* <p> [OVERRIDE] Log a message with error log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void errorImpl(Object message);
/**
* <p> [OVERRIDE] Log an error with error log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void errorImpl(Object message, Throwable t);
/**
* <p> [OVERRIDE] Log a message with fatal log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void fatalImpl(Object message);
/**
* <p> [OVERRIDE] Log an error with fatal log level.
* Subclasses should override this method to implement the logging.</p>
*/
protected abstract void fatalImpl(Object message, Throwable t);
/**
* <p> Are debug messages currently enabled? </p>
*
* <p> Subclasses should override this method if their logger provides
* a special implementation. </p>
*
* @return true
*/
protected boolean isDebugEnabledImpl() {
return true;
}
/**
* <p> Are error messages currently enabled? </p>
*
* <p> Subclasses should override this method if their logger provides
* a special implementation. </p>
*
* @return true
*/
protected boolean isErrorEnabledImpl() {
return true;
}
/**
* <p> Are fatal messages currently enabled? </p>
*
* <p> Subclasses should override this method if their logger provides
* a special implementation. </p>
*
* @return true
*/
protected boolean isFatalEnabledImpl() {
return true;
}
/**
* <p> Are info messages currently enabled? </p>
*
* <p> Subclasses should override this method if their logger provides
* a special implementation. </p>
*
* @return true
*/
protected boolean isInfoEnabledImpl() {
return true;
}
/**
* <p> Are warn messages currently enabled? </p>
*
* <p> Subclasses should override this method if their logger provides
* a special implementation. </p>
*
* @return true
*/
protected boolean isWarnEnabledImpl() {
return true;
}
// ------------------------------------------------- Implementation Methods
/**
* 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);
}
}

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/Log.java,v 1.9 2002/01/17 01:47:49 craigmcc Exp $
* $Revision: 1.9 $
* $Date: 2002/01/17 01:47:49 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/Log.java,v 1.10 2002/01/17 22:55:43 rdonkin Exp $
* $Revision: 1.10 $
* $Date: 2002/01/17 22:55:43 $
*
* ====================================================================
*
@@ -83,27 +83,9 @@ package org.apache.commons.logging;
* the underlying logging implementation in use.</p>
*
* @author Rod Waldhoff
* @version $Id: Log.java,v 1.9 2002/01/17 01:47:49 craigmcc Exp $
* @version $Id: Log.java,v 1.10 2002/01/17 22:55:43 rdonkin Exp $
*/
public interface Log {
// ---------------------------------------------------- Log Level Constants
/** All logging level. */
public static final int ALL = Integer.MIN_VALUE;
/** "Debug" level logging. */
public static final int DEBUG = 10000;
/** "Info" level logging. */
public static final int INFO = 20000;
/** "Warn" level logging. */
public static final int WARN = 30000;
/** "Error" level logging. */
public static final int ERROR = 40000;
/** "Fatal" level logging. */
public static final int FATAL = 50000;
/** No logging level. */
public static final int OFF = Integer.MAX_VALUE;
// ----------------------------------------------------- Logging Properties

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.9 2002/01/17 01:47:49 craigmcc Exp $
* $Revision: 1.9 $
* $Date: 2002/01/17 01:47:49 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/LogSource.java,v 1.10 2002/01/17 22:55:43 rdonkin Exp $
* $Revision: 1.10 $
* $Date: 2002/01/17 22:55:43 $
*
* ====================================================================
*
@@ -94,22 +94,22 @@ import java.lang.reflect.InvocationTargetException;
* </ul>
*
* @author Rod Waldhoff
* @version $Id: LogSource.java,v 1.9 2002/01/17 01:47:49 craigmcc Exp $
* @version $Id: LogSource.java,v 1.10 2002/01/17 22:55:43 rdonkin Exp $
*/
public class LogSource {
// ------------------------------------------------------- Class Attributes
static protected HashMap _logs = new HashMap();
static protected HashMap logs = new HashMap();
/** 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;
static protected boolean jdk14IsAvailable = false;
/** Constructor for current log class */
static protected Constructor _logimplctor = null;
static protected Constructor logImplctor = null;
// ----------------------------------------------------- Class Initializers
@@ -119,23 +119,23 @@ public class LogSource {
// Is Log4J Available?
try {
if(null != Class.forName("org.apache.log4j.Category")) {
_log4jIsAvailable = true;
log4jIsAvailable = true;
} else {
_log4jIsAvailable = false;
log4jIsAvailable = false;
}
} catch (Throwable t) {
_log4jIsAvailable = false;
log4jIsAvailable = false;
}
// Is JDK 1.4 Logging Available?
try {
if(null != Class.forName("java.util.logging.Logger")) {
_jdk14IsAvailable = true;
jdk14IsAvailable = true;
} else {
_jdk14IsAvailable = false;
jdk14IsAvailable = false;
}
} catch (Throwable t) {
_jdk14IsAvailable = false;
jdk14IsAvailable = false;
}
// Set the default Log implementation
@@ -156,10 +156,10 @@ public class LogSource {
}
} else {
try {
if (_log4jIsAvailable) {
if (log4jIsAvailable) {
setLogImplementation
("org.apache.commons.logging.Log4JCategoryLog");
} else if (_jdk14IsAvailable) {
} else if (jdk14IsAvailable) {
setLogImplementation
("org.apache.commons.logging.Jdk14Logger");
} else {
@@ -205,9 +205,9 @@ public class LogSource {
Class logclass = Class.forName(classname);
Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
_logimplctor = logclass.getConstructor(argtypes);
logImplctor = logclass.getConstructor(argtypes);
} catch (Throwable t) {
_logimplctor = null;
logImplctor = null;
}
}
@@ -223,16 +223,16 @@ public class LogSource {
NoSuchMethodException, SecurityException {
Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
_logimplctor = logclass.getConstructor(argtypes);
logImplctor = logclass.getConstructor(argtypes);
}
/** Get a <code>Log</code> instance by class name */
static public Log getInstance(String name) {
Log log = (Log)(_logs.get(name));
Log log = (Log)(logs.get(name));
if(null == log) {
log = makeNewLogInstance(name);
_logs.put(name,log);
logs.put(name,log);
}
return log;
}
@@ -274,7 +274,7 @@ public class LogSource {
try {
Object[] args = new Object[1];
args[0] = name;
log = (Log)(_logimplctor.newInstance(args));
log = (Log)(logImplctor.newInstance(args));
} catch (Throwable t) {
log = null;
}
@@ -291,7 +291,7 @@ public class LogSource {
* all logs known to me.
*/
static public String[] getLogNames() {
return (String[])(_logs.keySet().toArray(new String[_logs.size()]));
return (String[])(logs.keySet().toArray(new String[logs.size()]));
}

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/Attic/SimpleLog.java,v 1.8 2002/01/17 01:47:49 craigmcc Exp $
* $Revision: 1.8 $
* $Date: 2002/01/17 01:47:49 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/Attic/SimpleLog.java,v 1.9 2002/01/17 22:55:43 rdonkin Exp $
* $Revision: 1.9 $
* $Date: 2002/01/17 22:55:43 $
*
* ====================================================================
*
@@ -85,7 +85,7 @@ import java.util.Date;
* <li><code>org.apache.commons.logging.simplelog.showlogname</code> -
* Set to <code>true</code> if you want the Log instance name to be
* included in output messages.</li>
* <li><code>org.apache.commons.logging.simplelog.showtime</code> -
* <li><code>org.apache.commons.logging.simplelog.showdatetime</code> -
* Set to <code>true</code> if you want the current date and time
* to be included in output messages.</li>
* </ul>
@@ -98,26 +98,39 @@ import java.util.Date;
* @author Rod Waldhoff
* @author Robert Burrell Donkin
*
* @version $Id: SimpleLog.java,v 1.8 2002/01/17 01:47:49 craigmcc Exp $
* @version $Id: SimpleLog.java,v 1.9 2002/01/17 22:55:43 rdonkin Exp $
*/
public class SimpleLog extends AbstractLog {
public class SimpleLog implements Log {
// ------------------------------------------------------- Class Attributes
/** All system properties used by <code>Simple</code> start with this */
static protected final String _prefix =
static protected final String systemPrefix =
"org.apache.commons.logging.simplelog.";
/** All system properties which start with {@link #_prefix} */
static protected final Properties _simplelogProps = new Properties();
/** 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;
static protected boolean showLogName = false;
/** Include the current time in the log message */
static protected boolean _showtime = false;
static protected boolean showDateTime = false;
/** Used to format times */
static protected DateFormat _df = null;
static protected DateFormat dateFormatter = null;
// ---------------------------------------------------- Log Level Constants
/** "Debug" level logging. */
public static final int LOG_LEVEL_DEBUG = 1;
/** "Info" level logging. */
public static final int LOG_LEVEL_INFO = 2;
/** "Warn" level logging. */
public static final int LOG_LEVEL_WARN = 3;
/** "Error" level logging. */
public static final int LOG_LEVEL_ERROR = 4;
/** "Fatal" level logging. */
public static final int LOG_LEVEL_FATAL = 5;
// ------------------------------------------------------------ Initializer
@@ -128,8 +141,8 @@ public class SimpleLog extends AbstractLog {
Enumeration enum = System.getProperties().propertyNames();
while(enum.hasMoreElements()) {
String name = (String)(enum.nextElement());
if(null != name && name.startsWith(_prefix)) {
_simplelogProps.setProperty(name,System.getProperty(name));
if(null != name && name.startsWith(systemPrefix)) {
simpleLogProps.setProperty(name,System.getProperty(name));
}
}
@@ -138,7 +151,7 @@ public class SimpleLog extends AbstractLog {
ClassLoader.getSystemResourceAsStream("simplelog.properties");
if(null != in) {
try {
_simplelogProps.load(in);
simpleLogProps.load(in);
in.close();
} catch(java.io.IOException e) {
// ignored
@@ -150,18 +163,18 @@ public class SimpleLog extends AbstractLog {
// ignored
}
_showlogname = "true".equalsIgnoreCase(
_simplelogProps.getProperty(
_prefix + "showlogname","true"));
showLogName = "true".equalsIgnoreCase(
simpleLogProps.getProperty(
systemPrefix + "showlogname","true"));
_showtime = "true".equalsIgnoreCase(
_simplelogProps.getProperty(
_prefix + "showdate","true"));
showDateTime = "true".equalsIgnoreCase(
simpleLogProps.getProperty(
systemPrefix + "showdatetime","true"));
if(_showtime) {
_df = new SimpleDateFormat(
_simplelogProps.getProperty(
_prefix + "dateformat","yyyy/MM/dd HH:mm:ss:SSS zzz"));
if(showDateTime) {
dateFormatter = new SimpleDateFormat(
simpleLogProps.getProperty(
systemPrefix + "dateformat","yyyy/MM/dd HH:mm:ss:SSS zzz"));
}
}
@@ -169,7 +182,9 @@ public class SimpleLog extends AbstractLog {
// ------------------------------------------------------------- Attributes
/** The name of this simple log instance */
protected String _name = null;
protected String logName = null;
/** The current log level */
protected int currentLogLevel;
// ------------------------------------------------------------ Constructor
@@ -181,39 +196,61 @@ public class SimpleLog extends AbstractLog {
*/
public SimpleLog(String name) {
_name = name;
logName = name;
// set initial log level
// set default log level to ERROR
setLevel(Log.ERROR);
setLevel(SimpleLog.LOG_LEVEL_ERROR);
// set log level from properties
String lvl = _simplelogProps.getProperty(_prefix + "log." + _name);
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(_prefix + "log." + name);
lvl = simpleLogProps.getProperty(systemPrefix + "log." + name);
i = String.valueOf(name).lastIndexOf(".");
}
if(null == lvl) {
lvl = _simplelogProps.getProperty(_prefix + "defaultlog");
lvl = simpleLogProps.getProperty(systemPrefix + "defaultlog");
}
if("debug".equalsIgnoreCase(lvl)) {
setLevel(Log.DEBUG);
setLevel(SimpleLog.LOG_LEVEL_DEBUG);
} else if("info".equalsIgnoreCase(lvl)) {
setLevel(Log.INFO);
setLevel(SimpleLog.LOG_LEVEL_INFO);
} else if("warn".equalsIgnoreCase(lvl)) {
setLevel(Log.WARN);
setLevel(SimpleLog.LOG_LEVEL_WARN);
} else if("error".equalsIgnoreCase(lvl)) {
setLevel(Log.ERROR);
setLevel(SimpleLog.LOG_LEVEL_ERROR);
} else if("fatal".equalsIgnoreCase(lvl)) {
setLevel(Log.FATAL);
setLevel(SimpleLog.LOG_LEVEL_FATAL);
}
}
// -------------------------------------------------------- Properties
/**
* <p> Set logging level. </p>
*
* @param level new logging level
*/
public void setLevel(int currentLogLevel) {
this.currentLogLevel = currentLogLevel;
}
/**
* <p> Get logging level. </p>
*/
public int getLevel() {
return currentLogLevel;
}
// -------------------------------------------------------- Logging Methods
@@ -227,23 +264,23 @@ public class SimpleLog extends AbstractLog {
StringBuffer buf = new StringBuffer();
// append date-time if so configured
if(_showtime) {
buf.append(_df.format(new Date()));
if(showDateTime) {
buf.append(dateFormatter.format(new Date()));
buf.append(" ");
}
// append a readable representation of the log leve
switch(type) {
case DEBUG: buf.append("[DEBUG] "); break;
case INFO: buf.append("[INFO] "); break;
case WARN: buf.append("[WARN] "); break;
case ERROR: buf.append("[ERROR] "); break;
case FATAL: buf.append("[FATAL] "); 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(_name)).append(" - ");
if(showLogName) {
buf.append(String.valueOf(logName)).append(" - ");
}
// append the message
@@ -262,75 +299,193 @@ public class SimpleLog extends AbstractLog {
}
// ----------------------------------------------------- Log Implementation
/**
* Prepare then call {@link #log}.
* Is the given log level currently enabled?
*
* @param logLevel is this level enabled?
*/
protected final void debugImpl(Object message) {
log(Log.DEBUG,message,null);
}
/**
* Prepare then call {@link #log}.
*/
protected final void debugImpl(Object message, Throwable t) {
log(Log.DEBUG,message,t);
}
/**
* Prepare then call {@link #log}.
*/
protected final void infoImpl(Object message) {
log(Log.INFO,message,null);
}
/**
* Prepare then call {@link #log}.
*/
protected final void infoImpl(Object message, Throwable t) {
log(Log.INFO,message,t);
}
/**
* Prepare then call {@link #log}.
*/
protected final void warnImpl(Object message) {
log(Log.WARN,message,null);
protected boolean isLevelEnabled(int logLevel) {
// log level are numerically ordered so can use simple numeric
// comparison
return (logLevel >= currentLogLevel);
}
/**
* Prepare then call {@link #log}.
*/
protected final void warnImpl(Object message, Throwable t) {
log(Log.WARN,message,t);
}
// -------------------------------------------------------- Log Implementation
/**
* Prepare then call {@link #log}.
* <p> Log a message with debug log level.</p>
*/
protected final void errorImpl(Object message) {
log(Log.ERROR,message,null);
public final void debug(Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
}
}
/**
* Prepare then call {@link #log}.
*/
protected final void errorImpl(Object message, Throwable t) {
log(Log.ERROR,message,t);
* <p> Log an error with debug log level.</p>
*/
public final void debug(Object message, Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
}
}
/**
* Prepare then call {@link #log}.
*/
protected final void fatalImpl(Object message) {
log(Log.FATAL,message,null);
}
/**
* Prepare then call {@link #log}.
* <p> Log a message with info log level.</p>
*/
public final void info(Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO,message,null);
}
}
/**
* <p> Log an error with info log level.</p>
*/
protected final void fatalImpl(Object message, Throwable t) {
log(Log.FATAL,message,t);
public final void info(Object message, Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO, message, t);
}
}
/**
* <p> Log a message with warn log level.</p>
*/
public final void warn(Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, null);
}
}
/**
* <p> Log an error with warn log level.</p>
*/
public final void warn(Object message, Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, t);
}
}
/**
* <p> Log a message with error log level.</p>
*/
public final void error(Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, null);
}
}
/**
* <p> Log an error with error log level.</p>
*/
public final void error(Object message, Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, t);
}
}
/**
* <p> Log a message with fatal log level.</p>
*/
public final void fatal(Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, null);
}
}
/**
* <p> Log an error with fatal log level.</p>
*/
public final void fatal(Object message, Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, t);
}
}
/**
* <p> Are debug messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*/
public final boolean isDebugEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
}
/**
* <p> Are error messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*/
public final boolean isErrorEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
}
/**
* <p> Are fatal messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*/
public final boolean isFatalEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
}
/**
* <p> Are info messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*/
public final boolean isInfoEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
}
/**
* <p> Are warn messages currently enabled? </p>
*
* <p> This allows expensive operations such as <code>String</code>
* concatenation to be avoided when the message will be ignored by the
* logger. </p>
*/
public final boolean isWarnEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
}
}

View File

@@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/TestAll.java,v 1.1 2002/01/03 18:47:09 rdonkin Exp $
* $Revision: 1.1 $
* $Date: 2002/01/03 18:47:09 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/TestAll.java,v 1.2 2002/01/17 22:55:43 rdonkin Exp $
* $Revision: 1.2 $
* $Date: 2002/01/17 22:55:43 $
*
* ====================================================================
*
@@ -74,7 +74,7 @@ import junit.framework.*;
* coded by James Strachan. </p>
*
* @author Robert Burrell Donkin
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
public class TestAll extends TestCase {
@@ -86,8 +86,8 @@ public class TestAll extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
// Add independent test suites
suite.addTest(TestLogLevels.suite());
// don't have any tests anymore
//suite.addTest(TestLogLevels.suite());
return suite;
}

View File

@@ -1,147 +0,0 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/Attic/TestLog.java,v 1.1 2002/01/03 18:48:41 rdonkin Exp $
* $Revision: 1.1 $
* $Date: 2002/01/03 18:48:41 $
*
* ====================================================================
*
* 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;
/**
* <p> This logger simply uses the last message
* to set a property.
* This is used to test the log levels.
*
* @author Robert Burrell Donkin
* @version $Revision: 1.1 $
*/
public class TestLog extends AbstractLog {
private String message = null;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
private void setMessage(Object message) {
if (message == null) {
this.message = null;
} else {
this.message = message.toString();
}
}
protected void debugImpl(Object message) {
setMessage(message);
}
protected void debugImpl(Object message, Throwable t) {
debugImpl(message);
}
protected void infoImpl(Object message) {
setMessage(message);
}
protected void infoImpl(Object message, Throwable t) {
infoImpl(message);
}
protected void warnImpl(Object message) {
setMessage(message);
}
protected void warnImpl(Object message, Throwable t) {
warnImpl(message);
}
protected void errorImpl(Object message) {
setMessage(message);
}
protected void errorImpl(Object message, Throwable t) {
errorImpl(message);
}
protected void fatalImpl(Object message) {
setMessage(message);
}
protected void fatalImpl(Object message, Throwable t) {
fatalImpl(message);
}
}

View File

@@ -1,271 +0,0 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/Attic/TestLogLevels.java,v 1.1 2002/01/03 18:49:27 rdonkin Exp $
* $Revision: 1.1 $
* $Date: 2002/01/03 18:49:27 $
*
* ====================================================================
*
* 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 junit.framework.*;
/**
* <p>
*
* @author Robert Burrell Donkin
* @version $Revision: 1.1 $
*/
public class TestLogLevels extends TestCase {
public static Test suite() {
return new TestSuite(TestLogLevels.class);
}
public TestLogLevels(String testName) {
super(testName);
}
/**
* Test that <code>AbstractLog</code> correctly passes
* on calls as per current log level.
*/
public void testAbstractLogLevelCheck() {
TestLog log = new TestLog();
// test OFF log level
log.setLevel(Log.OFF);
log.setMessage(null);
log.debug("DEBUG");
assertNull(
"Log level checking failed (DEBUG/OFF)",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertNull(
"Log level checking failed (INFO/OFF)",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertNull(
"Log level checking failed (ERROR/OFF)",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertNull(
"Log level checking failed (FATAL/OFF)",
log.getMessage());
// test ALL log level
log.setLevel(Log.ALL);
log.setMessage(null);
log.debug("DEBUG");
assertEquals(
"Log level checking failed (DEBUG/ALL)",
"DEBUG",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertEquals(
"Log level checking failed (INFO/ALL)",
"INFO",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertEquals(
"Log level checking failed (ERROR/ALL)",
"ERROR",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertEquals(
"Log level checking failed (FATAL/ALL)",
"FATAL",
log.getMessage());
// test DEBUG log level
log.setLevel(Log.DEBUG);
log.setMessage(null);
log.debug("DEBUG");
assertEquals(
"Log level checking failed (DEBUG/DEBUG)",
"DEBUG",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertEquals(
"Log level checking failed (INFO/DEBUG)",
"INFO",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertEquals(
"Log level checking failed (ERROR/DEBUG)",
"ERROR",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertEquals(
"Log level checking failed (FATAL/DEBUG)",
"FATAL",
log.getMessage());
// test INFO log level
log.setLevel(Log.INFO);
log.setMessage(null);
log.debug("DEBUG");
assertNull(
"Log level checking failed (DEBUG/INFO)",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertEquals(
"Log level checking failed (INFO/INFO)",
"INFO",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertEquals(
"Log level checking failed (ERROR/INFO)",
"ERROR",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertEquals(
"Log level checking failed (FATAL/INFO)",
"FATAL",
log.getMessage());
// test ERROR log level
log.setLevel(Log.ERROR);
log.setMessage(null);
log.debug("DEBUG");
assertNull(
"Log level checking failed (DEBUG/ERROR)",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertNull(
"Log level checking failed (INFO/ERROR)",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertEquals(
"Log level checking failed (ERROR/ERROR)",
"ERROR",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertEquals(
"Log level checking failed (FATAL/ERROR)",
"FATAL",
log.getMessage());
// test FATAL log level
log.setLevel(Log.FATAL);
log.setMessage(null);
log.debug("DEBUG");
assertNull(
"Log level checking failed (DEBUG/FATAL)",
log.getMessage());
log.setMessage(null);
log.info("INFO");
assertNull(
"Log level checking failed (INFO/FATAL)",
log.getMessage());
log.setMessage(null);
log.error("ERROR");
assertNull(
"Log level checking failed (ERROR/FATAL)",
log.getMessage());
log.setMessage(null);
log.fatal("FATAL");
assertEquals(
"Log level checking failed (FATAL/FATAL)",
"FATAL",
log.getMessage());
}
}