refactored logging so that log4j is no longer a runtime depedency
updates to documentation to come git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138807 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
28
src/java/org/apache/commons/logging/Log.java
Normal file
28
src/java/org/apache/commons/logging/Log.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is published under the terms of the Apache Software License
|
||||||
|
* version 1.1, a copy of which has been included with this distribution in
|
||||||
|
* the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.httpclient.log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple logging interface abstracting log4j.
|
||||||
|
* @author Rod Waldhoff
|
||||||
|
* @version $Id: Log.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
|
||||||
|
*/
|
||||||
|
public interface Log {
|
||||||
|
public void assert(boolean assertion, String msg);
|
||||||
|
public void debug(Object message);
|
||||||
|
public void debug(Object message, Throwable t);
|
||||||
|
public void info(Object message);
|
||||||
|
public void info(Object message, Throwable t);
|
||||||
|
public void warn(Object message);
|
||||||
|
public void warn(Object message, Throwable t);
|
||||||
|
public void error(Object message);
|
||||||
|
public void error(Object message, Throwable t);
|
||||||
|
public void fatal(Object message);
|
||||||
|
public void fatal(Object message, Throwable t);
|
||||||
|
}
|
||||||
66
src/java/org/apache/commons/logging/Log4JCategoryLog.java
Normal file
66
src/java/org/apache/commons/logging/Log4JCategoryLog.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is published under the terms of the Apache Software License
|
||||||
|
* version 1.1, a copy of which has been included with this distribution in
|
||||||
|
* the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.httpclient.log;
|
||||||
|
|
||||||
|
import org.apache.log4j.Category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Rod Waldhoff
|
||||||
|
* @version $Id: Log4JCategoryLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
|
||||||
|
*/
|
||||||
|
public class Log4JCategoryLog implements Log {
|
||||||
|
Category _category = null;
|
||||||
|
|
||||||
|
public Log4JCategoryLog(String name) {
|
||||||
|
_category = Category.getInstance(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void assert(boolean assertion, String msg) {
|
||||||
|
_category.assert(assertion,msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void debug(Object message) {
|
||||||
|
_category.debug(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void debug(Object message, Throwable t) {
|
||||||
|
_category.debug(message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void info(Object message) {
|
||||||
|
_category.info(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void info(Object message, Throwable t) {
|
||||||
|
_category.info(message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void warn(Object message) {
|
||||||
|
_category.warn(message);
|
||||||
|
}
|
||||||
|
public final void warn(Object message, Throwable t) {
|
||||||
|
_category.warn(message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void error(Object message) {
|
||||||
|
_category.error(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void error(Object message, Throwable t) {
|
||||||
|
_category.error(message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void fatal(Object message) {
|
||||||
|
_category.fatal(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void fatal(Object message, Throwable t) {
|
||||||
|
_category.fatal(message,t);
|
||||||
|
}
|
||||||
|
}
|
||||||
95
src/java/org/apache/commons/logging/LogSource.java
Normal file
95
src/java/org/apache/commons/logging/LogSource.java
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is published under the terms of the Apache Software License
|
||||||
|
* version 1.1, a copy of which has been included with this distribution in
|
||||||
|
* the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.httpclient.log;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Rod Waldhoff
|
||||||
|
* @version $Id: LogSource.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
|
||||||
|
*/
|
||||||
|
public class LogSource {
|
||||||
|
static protected HashMap _logs = new HashMap();
|
||||||
|
static protected boolean _log4jIsAvailable = false;
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
if(null != Class.forName("org.apache.log4j.Category")) {
|
||||||
|
_log4jIsAvailable = true;
|
||||||
|
} else {
|
||||||
|
_log4jIsAvailable = false;
|
||||||
|
}
|
||||||
|
} catch(ClassNotFoundException e) {
|
||||||
|
_log4jIsAvailable = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LogSource() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Log getInstance(String name) {
|
||||||
|
Log log = (Log)(_logs.get(name));
|
||||||
|
if(null == log) {
|
||||||
|
log = makeNewLogInstance(name);
|
||||||
|
_logs.put(name,log);
|
||||||
|
}
|
||||||
|
return log;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Log getInstance(Class clazz) {
|
||||||
|
return getInstance(clazz.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link Log} implementation, based
|
||||||
|
* on the given <i>name</i>
|
||||||
|
* <p>
|
||||||
|
* The specific {@link Log} implementation returned
|
||||||
|
* is determined by the value of the
|
||||||
|
* <tt>httpclient.log</tt> property.
|
||||||
|
* The value of <tt>httpclient.log</tt> may be set to
|
||||||
|
* the fully specified name of a class that implements
|
||||||
|
* the {@link Log} interface. This class must also
|
||||||
|
* have a public constructor that takes a single
|
||||||
|
* {@link String} argument (containing the <i>name</i>
|
||||||
|
* of the {@link Log} to be constructed.
|
||||||
|
* <p>
|
||||||
|
* When <tt>httpclient.log</tt> is not set,
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @param name the log name (or category)
|
||||||
|
*/
|
||||||
|
static public Log makeNewLogInstance(String name) {
|
||||||
|
Log log = null;
|
||||||
|
String logclassname = System.getProperty("httpclient.log","org.apache.commons.httpclient.log.NoOpLog");
|
||||||
|
try {
|
||||||
|
Class logclass = Class.forName(logclassname);
|
||||||
|
Class[] argtypes = new Class[1];
|
||||||
|
argtypes[0] = "".getClass();
|
||||||
|
Constructor ctor = logclass.getConstructor(argtypes);
|
||||||
|
Object[] args = new Object[1];
|
||||||
|
args[0] = name;
|
||||||
|
log = (Log)(ctor.newInstance(args));
|
||||||
|
} catch(Exception e) {
|
||||||
|
log = null;
|
||||||
|
}
|
||||||
|
if(null == log) {
|
||||||
|
if(_log4jIsAvailable) {
|
||||||
|
return new Log4JCategoryLog(name);
|
||||||
|
} else {
|
||||||
|
log = new NoOpLog(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return log;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/java/org/apache/commons/logging/NoOpLog.java
Normal file
29
src/java/org/apache/commons/logging/NoOpLog.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is published under the terms of the Apache Software License
|
||||||
|
* version 1.1, a copy of which has been included with this distribution in
|
||||||
|
* the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.httpclient.log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Rod Waldhoff
|
||||||
|
* @version $Id: NoOpLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
|
||||||
|
*/
|
||||||
|
public final class NoOpLog implements Log {
|
||||||
|
public NoOpLog() { }
|
||||||
|
public NoOpLog(String name) { }
|
||||||
|
public void assert(boolean assertion, String msg) { }
|
||||||
|
public void debug(Object message) { }
|
||||||
|
public void debug(Object message, Throwable t) { }
|
||||||
|
public void info(Object message) { }
|
||||||
|
public void info(Object message, Throwable t) { }
|
||||||
|
public void warn(Object message) { }
|
||||||
|
public void warn(Object message, Throwable t) { }
|
||||||
|
public void error(Object message) { }
|
||||||
|
public void error(Object message, Throwable t) { }
|
||||||
|
public void fatal(Object message) { }
|
||||||
|
public void fatal(Object message, Throwable t) { }
|
||||||
|
}
|
||||||
165
src/java/org/apache/commons/logging/SimpleLog.java
Normal file
165
src/java/org/apache/commons/logging/SimpleLog.java
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is published under the terms of the Apache Software License
|
||||||
|
* version 1.1, a copy of which has been included with this distribution in
|
||||||
|
* the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.httpclient.log;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Rod Waldhoff
|
||||||
|
* @version $Id: SimpleLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
|
||||||
|
*/
|
||||||
|
public class SimpleLog implements Log {
|
||||||
|
static protected final Properties _simplelogProps = new Properties();
|
||||||
|
static protected boolean _showlogname = false;
|
||||||
|
static protected boolean _showtime = false;
|
||||||
|
static protected DateFormat _df = null;
|
||||||
|
|
||||||
|
static {
|
||||||
|
// add all system props that start with "httpclient."
|
||||||
|
Enumeration enum = System.getProperties().propertyNames();
|
||||||
|
while(enum.hasMoreElements()) {
|
||||||
|
String name = (String)(enum.nextElement());
|
||||||
|
if(null != name && name.startsWith("httpclient.")) {
|
||||||
|
_simplelogProps.setProperty(name,System.getProperty(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add props from the resource simplelog.properties
|
||||||
|
InputStream in = ClassLoader.getSystemResourceAsStream("simplelog.properties");
|
||||||
|
if(null != in) {
|
||||||
|
try {
|
||||||
|
_simplelogProps.load(in);
|
||||||
|
in.close();
|
||||||
|
} catch(java.io.IOException e) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
} catch(Throwable t) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
_showlogname = "true".equalsIgnoreCase(_simplelogProps.getProperty("httpclient.simplelog.showlogname","true"));
|
||||||
|
_showtime = "true".equalsIgnoreCase(_simplelogProps.getProperty("httpclient.simplelog.showdate","true"));
|
||||||
|
if(_showtime) {
|
||||||
|
_df = new SimpleDateFormat(_simplelogProps.getProperty("httpclient.simplelog.dateformat","yyyy/MM/dd HH:mm:ss:SSS zzz"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static final int DEBUG = 5;
|
||||||
|
protected static final int INFO = 4;
|
||||||
|
protected static final int WARN = 3;
|
||||||
|
protected static final int ERROR = 2;
|
||||||
|
protected static final int FATAL = 1;
|
||||||
|
protected int _logLevel = 2;
|
||||||
|
|
||||||
|
protected String _name = null;
|
||||||
|
|
||||||
|
public SimpleLog(String name) {
|
||||||
|
_name = name;
|
||||||
|
|
||||||
|
String lvl = _simplelogProps.getProperty("httpclient.simplelog.log." + _name);
|
||||||
|
int i = String.valueOf(name).lastIndexOf(".");
|
||||||
|
while(null == lvl && i > -1) {
|
||||||
|
name = name.substring(0,i);
|
||||||
|
lvl = _simplelogProps.getProperty("httpclient.simplelog.log." + name);
|
||||||
|
i = String.valueOf(name).lastIndexOf(".");
|
||||||
|
}
|
||||||
|
if(null == lvl) {
|
||||||
|
lvl = _simplelogProps.getProperty("httpclient.simplelog.defaultlog");
|
||||||
|
}
|
||||||
|
|
||||||
|
if("debug".equalsIgnoreCase(lvl)) {
|
||||||
|
_logLevel = DEBUG;
|
||||||
|
} else if("info".equalsIgnoreCase(lvl)) {
|
||||||
|
_logLevel = INFO;
|
||||||
|
} else if("warn".equalsIgnoreCase(lvl)) {
|
||||||
|
_logLevel = WARN;
|
||||||
|
} else if("error".equalsIgnoreCase(lvl)) {
|
||||||
|
_logLevel = ERROR;
|
||||||
|
} else if("fatal".equalsIgnoreCase(lvl)) {
|
||||||
|
_logLevel = FATAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void log(int type, Object message, Throwable t) {
|
||||||
|
if(_logLevel >= type) {
|
||||||
|
StringBuffer buf = new StringBuffer();
|
||||||
|
if(_showtime) {
|
||||||
|
buf.append(_df.format(new Date()));
|
||||||
|
buf.append(" ");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
if(_showlogname) {
|
||||||
|
buf.append(String.valueOf(_name)).append(" - ");
|
||||||
|
}
|
||||||
|
buf.append(String.valueOf(message));
|
||||||
|
if(t != null) {
|
||||||
|
buf.append(" <");
|
||||||
|
buf.append(t.toString());
|
||||||
|
buf.append(">");
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println(buf.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void assert(boolean assertion, String msg) {
|
||||||
|
if(!assertion) { error(msg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void debug(Object message) {
|
||||||
|
log(DEBUG,message,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void debug(Object message, Throwable t) {
|
||||||
|
log(DEBUG,message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void info(Object message) {
|
||||||
|
log(INFO,message,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void info(Object message, Throwable t) {
|
||||||
|
log(INFO,message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void warn(Object message) {
|
||||||
|
log(WARN,message,null);
|
||||||
|
}
|
||||||
|
public final void warn(Object message, Throwable t) {
|
||||||
|
log(WARN,message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void error(Object message) {
|
||||||
|
log(ERROR,message,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void error(Object message, Throwable t) {
|
||||||
|
log(ERROR,message,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void fatal(Object message) {
|
||||||
|
log(FATAL,message,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void fatal(Object message, Throwable t) {
|
||||||
|
log(FATAL,message,t);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user