Mybatis Log的实现流程

我们从Mybatis配置文件中的日志配置开始,来看看它到底是怎么实现的

image

<configuration>

    <settings>
    	<setting name="logImpl" value="NO_LOGGING"/>
    </settings>

</configuration>
从测试代码入手
  @Test
  public void shouldReadLogImplFromSettings() throws Exception {
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/logging/mybatis-config.xml");
    new SqlSessionFactoryBuilder().build(reader);
    reader.close(); 
    
    Log log = LogFactory.getLog(Object.class);
    log.debug("Debug message.");
    assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
  }
  • SqlSessionFactoryBuilder读取mybatis-config.xml配置文件的具体过程:
    • XmlConfigBuilder读取mybatis-config.xml文件中的setting配置,通过持有configuration对象来设置日志的实现
	configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));

在初始化配置过程中,Builder还初始化了以下对象中的数据:

 public BaseBuilder(Configuration configuration) {
    this.configuration = configuration;
    this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
    this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
  }
    • Configuration在实例化时注册了常用的日志实现类,并且实现了setLogImpl来指定具体的日志实现类
	// 注册常用的日志类
   typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
    typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
    typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
    typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
    typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
    typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
    typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);
	// 指定具体的日志实现类
	public void setLogImpl(Class<?> logImpl) {
	    if (logImpl != null) {
	      this.logImpl = (Class<? extends Log>) logImpl;
	      LogFactory.useCustomLogging(this.logImpl);
	    }
	  }
    //LogFactory中useCustomLogging的方法:
    public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
        setImplementation(clazz);
  }
    • LogFactory获取具体的Log实例:LogFactory持有Log具体实现的顶级接口,通过此接口可以实例化具体的Log实现类。
 private static Constructor<? extends Log> logConstructor;
 //设置实现类的方法:
   private static void setImplementation(Class<? extends Log> implClass) {
    try {
      Constructor<? extends Log> candidate = implClass.getConstructor(new Class[] { String.class });
      Log log = candidate.newInstance(new Object[] { LogFactory.class.getName() });
      log.debug("Logging initialized using '" + implClass + "' adapter.");
      //设置logConstructor,一旦设上,表明找到相应的log的jar包了,那后面别的log就不找了。
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }
  
   //根据传入的类名来构建Log
  public static Log getLog(String logger) {
    try {
      //构造函数,参数必须是一个,为String型,指明logger的名称
      return logConstructor.newInstance(new Object[] { logger });
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }
    • 具体实现:Log4j,slf4j等日志都实现了上一步的Log接口,例如:
public class Log4jImpl implements Log 

转载于:https://my.oschina.net/lfy2008/blog/1931205

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值