测试 Apache-common-logging 的使用:
调试程序:
1、Log log = LogFactory.getLog(LogTest.class);
2、log.error()
=================================
程序执行到 1 时, F5
调用 LogFactory中的: 创建一个指定clazz(名字)的Log
public static Log getLog(Class clazz)
throws LogConfigurationException {
return (getFactory().getInstance(clazz));
}
执行到 2 时: F5
程序进入 public class Jdk14Logger implements Log, Serializable 中的
public void error(Object message) {
log(Level.SEVERE, String.valueOf(message), null);
}
其中 Logger 为 java.util.logging.Logger
private void log( Level level, String msg, Throwable ex ) {
Logger logger = getLogger();
if (logger.isLoggable(level)) {
// Hack (?) to get the stack trace.
Throwable dummyException=new Throwable();
StackTraceElement locations[]=dummyException.getStackTrace();
// Caller will be the third element
String cname="unknown";
String method="unknown";
if( locations!=null && locations.length >2 ) {
StackTraceElement caller=locations[2];
cname=caller.getClassName();
method=caller.getMethodName();
}
if( ex==null ) {
logger.logp( level, cname, method, msg );
} else {
logger.logp( level, cname, method, msg, ex );
}
}
}
public Logger getLogger() {
if (logger == null) {
logger = Logger.getLogger(name);
}
return (logger);
}
本文介绍Apache-common-logging的使用方法,通过示例演示了如何在Java中使用Jdk14Logger进行错误日志记录,包括获取Logger实例及记录错误日志的具体流程。
3872

被折叠的 条评论
为什么被折叠?



