rdonkin 02/01/17 14:55:43
Modified: logging/src/java/org/apache/commons/logging Log.java
LogSource.java SimpleLog.java
logging/src/test/org/apache/commons/logging TestAll.java
Removed: logging/src/java/org/apache/commons/logging AbstractLog.java
logging/src/test/org/apache/commons/logging TestLog.java
TestLogLevels.java
Log:
Removed AbstractLog and associated test case, removed log level constants from Log
interface, added renamed level constants to SimpleLog and fixed SimpleLog
configuration bug
Revision Changes Path
1.10 +4 -22
jakarta-commons/logging/src/java/org/apache/commons/logging/Log.java
Index: Log.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/Log.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Log.java 17 Jan 2002 01:47:49 -0000 1.9
+++ Log.java 17 Jan 2002 22:55:43 -0000 1.10
@@ -1,7 +1,7 @@
/*
- * $Header:
/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/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 @@
* 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
1.10 +23 -23
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.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- LogSource.java 17 Jan 2002 01:47:49 -0000 1.9
+++ LogSource.java 17 Jan 2002 22:55:43 -0000 1.10
@@ -1,7 +1,7 @@
/*
- * $Header:
/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/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 @@
* </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 @@
// 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 @@
}
} 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 @@
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 @@
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 @@
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 @@
* 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()]));
}
1.9 +231 -76
jakarta-commons/logging/src/java/org/apache/commons/logging/SimpleLog.java
Index: SimpleLog.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/SimpleLog.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SimpleLog.java 17 Jan 2002 01:47:49 -0000 1.8
+++ SimpleLog.java 17 Jan 2002 22:55:43 -0000 1.9
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/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/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/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 @@
* <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 @@
* @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 @@
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 @@
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 @@
// 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 @@
// ------------------------------------------------------------- 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 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 @@
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 @@
}
- // ----------------------------------------------------- Log Implementation
+ /**
+ * 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);
+ }
+
+
+ // -------------------------------------------------------- Log Implementation
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log a message with debug log level.</p>
*/
- protected final void debugImpl(Object message) {
- log(Log.DEBUG,message,null);
+ public final void debug(Object message) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
+ log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
+ }
}
+
+
+ /**
+ * <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}.
+ * <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 debugImpl(Object message, Throwable t) {
- log(Log.DEBUG,message,t);
+ public final void info(Object message, Throwable t) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
+ log(SimpleLog.LOG_LEVEL_INFO, message, t);
+ }
}
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log a message with warn log level.</p>
*/
- protected final void infoImpl(Object message) {
- log(Log.INFO,message,null);
+ public final void warn(Object message) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
+ log(SimpleLog.LOG_LEVEL_WARN, message, null);
+ }
}
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log an error with warn log level.</p>
*/
- protected final void infoImpl(Object message, Throwable t) {
- log(Log.INFO,message,t);
+ public final void warn(Object message, Throwable t) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
+ log(SimpleLog.LOG_LEVEL_WARN, message, t);
+ }
}
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log a message with error log level.</p>
*/
- protected final void warnImpl(Object message) {
- log(Log.WARN,message,null);
+ public final void error(Object message) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
+ log(SimpleLog.LOG_LEVEL_ERROR, message, null);
+ }
}
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log an error with error log level.</p>
*/
- protected final void warnImpl(Object message, Throwable t) {
- log(Log.WARN,message,t);
+ public final void error(Object message, Throwable t) {
+
+ if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
+ log(SimpleLog.LOG_LEVEL_ERROR, message, t);
+ }
}
+
/**
- * Prepare then call {@link #log}.
+ * <p> Log a message with fatal log level.</p>
*/
- protected final void errorImpl(Object message) {
- log(Log.ERROR,message,null);
+ 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);
+ }
+ }
+
+
/**
- * Prepare then call {@link #log}.
+ * <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>
*/
- protected final void errorImpl(Object message, Throwable t) {
- log(Log.ERROR,message,t);
+ 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);
+ }
+
/**
- * Prepare then call {@link #log}.
+ * <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>
*/
- protected final void fatalImpl(Object message) {
- log(Log.FATAL,message,null);
+ 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);
+ }
+
/**
- * Prepare then call {@link #log}.
+ * <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>
*/
- protected final void fatalImpl(Object message, Throwable t) {
- log(Log.FATAL,message,t);
+ public final boolean isWarnEnabled() {
+
+ return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
}
}
+
1.2 +5 -5
jakarta-commons/logging/src/test/org/apache/commons/logging/TestAll.java
Index: TestAll.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/logging/src/test/org/apache/commons/logging/TestAll.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestAll.java 3 Jan 2002 18:47:09 -0000 1.1
+++ TestAll.java 17 Jan 2002 22:55:43 -0000 1.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/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/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 @@
* 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 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;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>