MyBatis系列-Mybatis源碼之 Log的實現流程(4)

本篇文章內容

  • Mybatis的日誌如何配置,如何加載配置?
  • 核心接口和實現類
  • 如何實現只打印SQL,不打印結果集?
  • 如何實現只打印部分Mapper的SQL?
官方文檔:

http://www.mybatis.org/mybatis-3/zh/logging.htmlhtml

從配置開始

咱們從Mybatis配置文件中的日誌配置開始,來看看它究竟是怎麼實現的 imagesql

<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
日誌打印是如何實現的?

Mybatis在獲取執行一條SQL語句的時候,對Connection,Statement,ResultSet,PreparedStatement做了代理,經過代理實現實現了日誌打印。 imageapache

ConnectionLogger
  • 描述:獲取數據庫鏈接的日誌代理類
  • 打印哪些方法的SQL:prepareStatement,createStatement,prepareCall,打印select。
  • 調用鏈:session.selectList-->configuration.getMappedStatement獲取MappedStatement-->BaseExecutor.query-->BaseExecutor.getConnection(MappedStatement.statementLog)
  • BaseExecutor.getConnection會根據Log的具體狀況返回是否生成ConnectionLogger
protected Connection getConnection(Log statementLog) throws SQLException {
    Connection connection = transaction.getConnection();
    if (statementLog.isDebugEnabled()) {
      //若是須要打印Connection的日誌,返回一個ConnectionLogger(代理模式)
      return ConnectionLogger.newInstance(connection, statementLog, queryStack);
    } else {
      return connection;
    }
  }
PreparedStatementLogger、StatementLogger
  • 描述:PreparedStatement,Statement代理類。
  • 打印哪些方法的SQL:execute、executeQuery、executeUpdate、addBatch
ResultSetLog
  • 描述:ResultSet代理類
  • 打印哪些方法的SQL: 調用ResultSet.next()時打印結果集,表頭。只有當Log接口的實現類返isTraceEnabled返回true時纔打印。所以,能夠經過配置Log實現類對應的日誌級別來設置是否打印結果集
//log4j爲例
log4j.logger.org.mybatis.example=TRACE
能否經過調用方法開啓/關閉Mybatis的日誌打印?

經過Excutor接口發現,執行SQL的MappedStatement都持有一個Log接口,這個接口的具體設置是在其Builder中建立,也就是說只有在Builder的時候設置才生效。而在獲取Log的時候直接返回了設置的statementLog,而且MappedStatement的持有的Log爲private,也沒有提供公共的設置方法,所以,只能經過修改源代碼的方式來設置Logsession

public final class MappedStatement {
    //持有的Log接口
    private Log statementLog;
    ......
    public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
      mappedStatement.configuration = configuration;
    .  ....
      String logId = id;
      if (configuration.getLogPrefix() != null) {
        logId = configuration.getLogPrefix() + id;
      }
      mappedStatement.statementLog = LogFactory.getLog(logId);
      mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
    }
    
    //獲取Log時調用:
      public Log getStatementLog() {
         return statementLog;
      }
      //修改成經過LogFactory獲取
      public Log getStatementLog() {
           String logId = this.getId();
            if (configuration.getLogPrefix() != null) {
              logId = configuration.getLogPrefix() + id;
            }
            return LogFactory.getLog(logId);
      }
}

動態修改Log打印實現:mybatis

@RequestMapping("/log/{type}")
    public Result log(@PathVariable("type")String type) {
        if("on".equals(type)){
            LogFactory.useStdOutLogging();
        }else {
            LogFactory.useSlf4jLogging();
        }
        return new Result().success();
    }
相關文章
相關標籤/搜索