Mybaits 基础组件---日志分析第三章

LogFactory 日志工厂

日志组件: 主要使用的工厂设计模式、适配器设计模式

包目录
日志组件
mybaits为了兼容其他第三方组件,自己定义一个针对于mybaits的通用接口

public interface Log {

  boolean isDebugEnabled();

  boolean isTraceEnabled();

  void error(String s, Throwable e);

  void error(String s);

  void debug(String s);

  void trace(String s);

  void warn(String s);

}

顺序加载

 static {
// 第一步: 加载slf4f
    tryImplementation(LogFactory::useSlf4jLogging);
    // 第二步: commonLogging
    tryImplementation(LogFactory::useCommonsLogging);
    // 第三步 log4f2
    tryImplementation(LogFactory::useLog4J2Logging);
    tryImplementation(LogFactory::useLog4JLogging);
    tryImplementation(LogFactory::useJdkLogging);
    tryImplementation(LogFactory::useNoLogging);
  }

如果当前类下有就加载。

myabtis 如果当前类下面无法找到该组件应该怎么办

 tryImplementation(LogFactory::useSlf4jLogging);

看一个简单源码日志分析

 public static synchronized void useSlf4jLogging() {
    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  }

  private static void setImplementation(Class<? extends Log> implClass) {
    try {
      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
      Log log = candidate.newInstance(LogFactory.class.getName());
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");
      }
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }
 private static void tryImplementation(Runnable runnable) {
 // 判断是否为null 如果为null 加载类 如果不为null 就不加载。
    if (logConstructor == null) {
      try {
      //然后运行该对象
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }

这样做的目前就是在当前类下面如果没有找到继续加载下一个 ( ignore)了异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值