探究JDK-logging

本文介绍Apache-common-logging的使用方法,通过示例演示了如何在Java中使用Jdk14Logger进行错误日志记录,包括获取Logger实例及记录错误日志的具体流程。

测试 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);
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值