org.apache.commons.logging的用法(下)

完全可以分装一个类,而不必每个类要log,都得
定义private static Log testLogger = LogFactory.getLog(xxx.class);

最普通的实现:
package moxtv.central.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
*
* @author weip
* @time 2006-4-30 16:38:27
*
*/
public class Logger {

/**
*
*/
private static Log sysLogger = LogFactory.getLog(Logger.class);

/**
*
* @author weip
* @time 2006-4-30 16:39:57
* @param obj
* Object
*/
public static void debug(Object obj) {
sysLogger.debug(obj);
}

/**
*
* @author weip
* @time 2006-4-30 16:41:39
* @param obj
* Object
*/
public static void info(Object obj) {
sysLogger.info(obj);
}

/**
*
* @author weip
* @time 2006-4-30 16:42:28
* @param obj
* Object
*/
public static void warn(Object obj) {
sysLogger.warn(obj);
}

/**
*
* @author weip
* @time 2006-4-30 16:42:50
* @param obj
* Object
*/
public static void error(Object obj) {
sysLogger.error(obj);
}

/**
*
* @author weip
* @time 2006-4-30 16:42:58
* @param obj
* Object
*/
public static void fatal(Object obj) {
sysLogger.fatal(obj);
}

}

另外一种重构后的实现,但似乎也没有带来更大的益处
package moxtv.central.util;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Logger的重构,也许不大合适
* @author weip
* @time 2006-4-30 17:24:49
*
*/
public class Logger2 {

/**
*
*/
private static Log sysLogger = LogFactory.getLog(Logger.class);
/**
*
*/
public static final String DEBUG = "debug";

/**
*
*/
public static final String INFO = "info";

/**
*
*/
public static final String WARN = "warn";

/**
*
*/
public static final String ERROR = "error";

/**
*
*/
public static final String FATAL = "fatal";

/**
* generic method instead of the five methods above
*
* @author weip
* @time 2006-4-30 16:51:31
* @param obj
* Object
* @param level
* String
*
*/
public static void log(Object obj, String level) {

Method method;
try {
method = sysLogger.getClass().getMethod(level,
new Class[] {Object.class });

method.invoke(sysLogger, new Object[] {obj });
} catch (Exception e) {

e.printStackTrace();
throw new RuntimeException("It should be a unreachalbe point");
}
}
}

你可能感兴趣的:(commons)