在 Java 开发中常用的日志框架有 Log4j、Log4j2、Apache Common Log、java.util.logging、slf4j 等,这些日志框架对外提供的接口各不相同。本章详细描述 MyBatis 是如何通过适配器的方式集成和复用这些第三方框架的。

日志适配器

MyBatis 的日志模块位于 org.apache.ibatis.logging 包中,该模块中 Log 接口定义了日志模块的功能,然后分别为不同的日志框架定义不同的日志适配器,这些日志适配器都继承 Log 接口,LogFactory 工厂负责创建对应的日志框架适配器。

image.png

下面来看 jdk14 日志适配器模式的类图:

image.png

在 LogFactory 类加载时会执行其静态代码快,按照顺序加载并实例化对应日志框架的适配器,然后用 logConstructor 字段记录当前使用的第三方日志框架的适配器。

tryImplementation() 方法使用 try cache 捕获了加载日志框架过程中产生的异常信息,且在 cache 中没做任何操作,所以不会有异常信息抛出,正常执行。如果没有引入任何日志框架,会使用 useJdkLogging ,这是 JDK 自带的日志工具。
_

  1. public final class LogFactory {
  2. /**
  3. * Marker to be used by logging implementations that support markers
  4. */
  5. public static final String MARKER = "MYBATIS";
  6. // 记录当前使用的第三方日志框架的适配器的构造方法
  7. private static Constructor<? extends Log> logConstructor;
  8. // 尝试加载每种日志框架,调用顺序是:
  9. // useSlf4jLogging --> useCommonsLogging --> useLog4J2Logging -->
  10. // useLog4JLogging --> useJdkLogging --> useNoLogging
  11. static {
  12. tryImplementation(new Runnable() {
  13. @Override
  14. public void run() {
  15. useSlf4jLogging();
  16. }
  17. });
  18. tryImplementation(new Runnable() {
  19. @Override
  20. public void run() {
  21. useCommonsLogging();
  22. }
  23. });
  24. tryImplementation(new Runnable() {
  25. @Override
  26. public void run() {
  27. useLog4J2Logging();
  28. }
  29. });
  30. tryImplementation(new Runnable() {
  31. @Override
  32. public void run() {
  33. useLog4JLogging();
  34. }
  35. });
  36. tryImplementation(new Runnable() {
  37. @Override
  38. public void run() {
  39. useJdkLogging();
  40. }
  41. });
  42. tryImplementation(new Runnable() {
  43. @Override
  44. public void run() {
  45. useNoLogging();
  46. }
  47. });
  48. }
  49. private LogFactory() {
  50. // disable construction
  51. }
  52. public static Log getLog(Class<?> aClass) {
  53. return getLog(aClass.getName());
  54. }
  55. public static Log getLog(String logger) {
  56. try {
  57. return logConstructor.newInstance(logger);
  58. } catch (Throwable t) {
  59. throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t);
  60. }
  61. }
  62. public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
  63. setImplementation(clazz);
  64. }
  65. public static synchronized void useSlf4jLogging() {
  66. setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  67. }
  68. public static synchronized void useCommonsLogging() {
  69. setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  70. }
  71. public static synchronized void useLog4JLogging() {
  72. setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  73. }
  74. public static synchronized void useLog4J2Logging() {
  75. setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  76. }
  77. public static synchronized void useJdkLogging() {
  78. setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  79. }
  80. public static synchronized void useStdOutLogging() {
  81. setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  82. }
  83. public static synchronized void useNoLogging() {
  84. setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  85. }
  86. // 尝试加载日志框架
  87. private static void tryImplementation(Runnable runnable) {
  88. if (logConstructor == null) {
  89. try {
  90. runnable.run();
  91. } catch (Throwable t) {
  92. // 加载异常被忽略了
  93. // ignore
  94. }
  95. }
  96. }
  97. private static void setImplementation(Class<? extends Log> implClass) {
  98. try {
  99. //获取指定适配器的构造方法
  100. Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
  101. // 实例化适配器
  102. Log log = candidate.newInstance(LogFactory.class.getName());
  103. // 输出日志
  104. if (log.isDebugEnabled()) {
  105. log.debug("Logging initialized using '" + implClass + "' adapter.");
  106. }
  107. // 初始化logConstructor字段
  108. logConstructor = candidate;
  109. } catch (Throwable t) {
  110. throw new LogException("Error setting Log implementation. Cause: " + t, t);
  111. }
  112. }
  113. }

引入 SLF4J 日志框架

SLF4J 只是日志框架统一的接口定义(参考:http://www.slf4j.org/manual.html),还需要引入实现,pom.xml 文件增加如下内容:

  1. <dependency>
  2. <groupId>org.slf4j</groupId>
  3. <artifactId>slf4j-api</artifactId>
  4. <version>1.7.28</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.slf4j</groupId>
  8. <artifactId>slf4j-simple</artifactId>
  9. <version>1.7.28</version>
  10. <scope>test</scope>
  11. </dependency>

在 LogFactory 类加载的时候,其静态代码块会将 logConstructor 初始化为 SLF4J 适配器的构造函数。

通过 simplelogger.properties 属性文件配置日志级别为 debug,属性文件的命名是固定的,参考 org.slf4j.impl.SimpleLoggerConfiguration 类:

image.png

执行一个简单的查询操作,会输出如下日志信息:

  1. [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
  2. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
  3. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
  4. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
  5. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
  6. [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
  7. [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1426329391.
  8. [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - ==> Preparing: select id, name, sex, selfcard_no, note from t_student where id = ?
  9. [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - ==> Parameters: 1(Long)
  10. [main] DEBUG com.yjw.mybatis.dao.StudentMapper.selectByPrimaryKey - <== Total: 1

Slf4jImpl 类实现了 org.apache.ibatis.logging.Log 接口,并封装了 SLF4J 的日志接口,Log 接口的功能全部通过调用 SLF4J 的日志接口实现。

Slf4jImpl 构造函数中实现了 SLF4J 的版本区分,根据不同的版本使用不同的接口实现日志功能,源码如下:

  1. public class Slf4jImpl implements Log {
  2. private Log log;
  3. public Slf4jImpl(String clazz) {
  4. Logger logger = LoggerFactory.getLogger(clazz);
  5. if (logger instanceof LocationAwareLogger) {
  6. try {
  7. // check for slf4j >= 1.6 method signature
  8. logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class);
  9. log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
  10. return;
  11. } catch (SecurityException e) {
  12. // fail-back to Slf4jLoggerImpl
  13. } catch (NoSuchMethodException e) {
  14. // fail-back to Slf4jLoggerImpl
  15. }
  16. }
  17. // Logger is not LocationAwareLogger or slf4j version < 1.6
  18. log = new Slf4jLoggerImpl(logger);
  19. }
  20. @Override
  21. public boolean isDebugEnabled() {
  22. return log.isDebugEnabled();
  23. }
  24. @Override
  25. public boolean isTraceEnabled() {
  26. return log.isTraceEnabled();
  27. }
  28. @Override
  29. public void error(String s, Throwable e) {
  30. log.error(s, e);
  31. }
  32. @Override
  33. public void error(String s) {
  34. log.error(s);
  35. }
  36. @Override
  37. public void debug(String s) {
  38. log.debug(s);
  39. }
  40. @Override
  41. public void trace(String s) {
  42. log.trace(s);
  43. }
  44. @Override
  45. public void warn(String s) {
  46. log.warn(s);
  47. }
  48. }

作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/vb9ehl 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。