在 Java 開發中常用的日誌框架有 Log4j、Log4j2、Apache Common Log、java.util.logging、slf4j 等,這些日誌框架對外提供的接口各不相同。本章詳細描述 MyBatis 是如何通過適配器的方式集成和複用這些第三方框架的。
日誌適配器
MyBatis 的日誌模塊位於 org.apache.ibatis.logging 包中,該模塊中 Log 接口定義了日誌模塊的功能,然後分別爲不同的日誌框架定義不同的日誌適配器,這些日誌適配器都繼承 Log 接口,LogFactory 工廠負責創建對應的日誌框架適配器。
下面來看 jdk14 日誌適配器模式的類圖:
在 LogFactory 類加載時會執行其靜態代碼快,按照順序加載並實例化對應日誌框架的適配器,然後用 logConstructor 字段記錄當前使用的第三方日誌框架的適配器。
tryImplementation() 方法使用 try cache 捕獲了加載日誌框架過程中產生的異常信息,且在 cache 中沒做任何操作,所以不會有異常信息拋出,正常執行。如果沒有引入任何日誌框架,會使用 useJdkLogging ,這是 JDK 自帶的日誌工具。
public final class LogFactory { /** * Marker to be used by logging implementations that support markers */ public static final String MARKER = "MYBATIS"; // 記錄當前使用的第三方日誌框架的適配器的構造方法 private static Constructor<? extends Log> logConstructor; // 嘗試加載每種日誌框架,調用順序是: // useSlf4jLogging --> useCommonsLogging --> useLog4J2Logging --> // useLog4JLogging --> useJdkLogging --> useNoLogging static { tryImplementation(new Runnable() { @Override public void run() { useSlf4jLogging(); } }); tryImplementation(new Runnable() { @Override public void run() { useCommonsLogging(); } }); tryImplementation(new Runnable() { @Override public void run() { useLog4J2Logging(); } }); tryImplementation(new Runnable() { @Override public void run() { useLog4JLogging(); } }); tryImplementation(new Runnable() { @Override public void run() { useJdkLogging(); } }); tryImplementation(new Runnable() { @Override public void run() { useNoLogging(); } }); } private LogFactory() { // disable construction } public static Log getLog(Class<?> aClass) { return getLog(aClass.getName()); } public static Log getLog(String logger) { try { return logConstructor.newInstance(logger); } catch (Throwable t) { throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t); } } public static synchronized void useCustomLogging(Class<? extends Log> clazz) { setImplementation(clazz); } public static synchronized void useSlf4jLogging() { setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class); } public static synchronized void useCommonsLogging() { setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class); } public static synchronized void useLog4JLogging() { setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class); } public static synchronized void useLog4J2Logging() { setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class); } public static synchronized void useJdkLogging() { setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class); } public static synchronized void useStdOutLogging() { setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class); } public static synchronized void useNoLogging() { setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class); } // 嘗試加載日誌框架 private static void tryImplementation(Runnable runnable) { if (logConstructor == null) { try { runnable.run(); } catch (Throwable t) { // 加載異常被忽略了 // ignore } } } 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字段 logConstructor = candidate; } catch (Throwable t) { throw new LogException("Error setting Log implementation. Cause: " + t, t); } } }
引入 SLF4J 日誌框架
SLF4J 只是日誌框架統一的接口定義(參考:http://www.slf4j.org/manual.html),還需要引入實現,pom.xml 文件增加如下內容:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.28</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.28</version> <scope>test</scope> </dependency>
在 LogFactory 類加載的時候,其靜態代碼塊會將 logConstructor 初始化爲 SLF4J 適配器的構造函數。
通過 simplelogger.properties 屬性文件配置日誌級別爲 debug,屬性文件的命名是固定的,參考 org.slf4j.impl.SimpleLoggerConfiguration 類:
執行一個簡單的查詢操作,會輸出如下日誌信息:
[main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1426329391. [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - ==> Preparing: select id, name, sex, selfcard_no, note from t_student where id = ? [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - ==> Parameters: 1(Long) [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - <== Total: 1
Slf4jImpl 類實現了 org.apache.ibatis.logging.Log 接口,並封裝了 SLF4J 的日誌接口,Log 接口的功能全部通過調用 SLF4J 的日誌接口實現。
Slf4jImpl 構造函數中實現了 SLF4J 的版本區分,根據不同的版本使用不同的接口實現日誌功能,源碼如下:
public class Slf4jImpl implements Log { private Log log; public Slf4jImpl(String clazz) { Logger logger = LoggerFactory.getLogger(clazz); if (logger instanceof LocationAwareLogger) { try { // check for slf4j >= 1.6 method signature logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class); log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger); return; } catch (SecurityException e) { // fail-back to Slf4jLoggerImpl } catch (NoSuchMethodException e) { // fail-back to Slf4jLoggerImpl } } // Logger is not LocationAwareLogger or slf4j version < 1.6 log = new Slf4jLoggerImpl(logger); } @Override public boolean isDebugEnabled() { return log.isDebugEnabled(); } @Override public boolean isTraceEnabled() { return log.isTraceEnabled(); } @Override public void error(String s, Throwable e) { log.error(s, e); } @Override public void error(String s) { log.error(s); } @Override public void debug(String s) { log.debug(s); } @Override public void trace(String s) { log.trace(s); } @Override public void warn(String s) { log.warn(s); } }
MyBatis 源碼篇