ibatis里面日志记录支持多种日志就起原理

ibatis里面提供了四中日志记录器,一种是apache的common loggin,一种是jdk的日志记录
一种是log4j,一种是自己实现的不记录。

那么ibats是怎么来实现不同种类来切换的呢?通过顺序加载每一种日志记录器来实现,也就是如果classpath下有哪种日志记录器的jar,就用哪种来实现,代码如下:

public class LogFactory {

private static Constructor logConstructor;

static {
tryImplementation("org.apache.commons.logging.LogFactory", "com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl");
tryImplementation("org.apache.log4j.Logger", "com.ibatis.common.logging.log4j.Log4jImpl");
tryImplementation("java.util.logging.Logger", "com.ibatis.common.logging.jdk14.Jdk14LoggingImpl");
tryImplementation("java.lang.Object", "com.ibatis.common.logging.nologging.NoLoggingImpl");
}

private static void tryImplementation(String testClassName, String implClassName) {
if (logConstructor == null) {
try {
Resources.classForName(testClassName);
Class implClass = Resources.classForName(implClassName);
logConstructor = implClass.getConstructor(new Class[]{Class.class});
} catch (Throwable t) {
}
}
}


从上面可以看出来,是利用了classLoad来实现的,如果没有加载到就抛出异常,说明不采用当然的日志记录器,如果我们在lib目录下什么都没有放,那么根据代码肯定是加载了jdk的日志记录器,所以构造方法就不会为空,最终就是用的jdk来实现的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值