mybatis 本身没有提供日志的实现类,使用的是第三方组件。
为了匹配上不同的第三方组件,mybatis 使用的是适配器模式解决第三方组件日志级别与mybatis 日志级别不同的问题。
而在第三方组件的具体实现时,某些日志组件的实现又使用了代理模式。
mybatis虽然没有提供日志的实现类,但是定义了Mybatis自己的日志级别:error,debug,trace,warn.
mybaits Log源码
package org.apache.ibatis.logging;
public interface Log {
boolean isDebugEnabled();
boolean isTraceEnabled();
void error(String var1, Throwable var2);
void error(String var1);
void debug(String var1);
void trace(String var1);
void warn(String var1);
}
mybatis 会依据顺序slf4J → commonsLoging → Log4J2 → Log4J → JdkLog 依次查找这些组件是否被提供。因此如果配置了slf4j 还配置了Log4J,mybatis 只会采用slf4j的日志组件。
适配器模式会提供一个适配器,而适配器是作为两个不兼容的接口之间的桥梁,将一个类的接口转换成客户希望的另外一个接口。
在mybatis 日志管理模块中,就是把第三方组件的日志级别适配成mybatis定义的日志级别。
给目标对象提供一个代理对象,并由代理对象控制对目标对象的引用。
在mybatis 日志模块中就是有代理对象LogFactory 去控制调用具体的第三方日志组件。
日志组件实现的步骤:
第一步:LogFactory 会根据条件logConstructor ==null 去判断是否获得了其中某一个类的构造方法,如果获得了,则通过获取到的构造方法去实例化对象,在获取实例时,不同的日志组件有不同的操作:
1、使用Slf4j和Log4j2的组件时,在LogFactory中获取到的是一个代理对象,然后通过代理对象获取到具体的实现类
2、commonsLoging、jdkLog、Log4j,则是直接获取到某一个具体的实现类,这是由于日志组件本身的原因。
而这些具体的实现类其实是一个适配器,将组件日志(Slf4j、Log4j2、commonsLoging、jdkLog、Log4j)转换成mybatis定义的日志。
1、LogFactory源码:
package org.apache.ibatis.logging;
import java.lang.reflect.Constructor;
import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;
import org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.logging.nologging.NoLoggingImpl;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
public final class LogFactory {
public static final String MARKER = "MYBATIS";
//具体使用的某个日志组件的构造方法
private static Constructor extends Log> logConstructor;
//不能自己实例化,需要使用单例模式
private LogFactory() {
}
public static Log getLog(Class> aClass) {
return getLog(aClass.getName());
}
public static Log getLog(String logger) {
try {
return (Log)logConstructor.newInstance(new Object[]{logger});
} catch (Throwable var2) {
throw new LogException("Error creating logger for logger " + logger + ". Cause: " + var2, var2);
}
}
// 提供一个扩展功能,自定义log
public static synchronized void useCustomLogging(Class extends Log> clazz) {
setImplementation(clazz);
}
public static synchronized void useSlf4jLogging() {
setImplementation(Slf4jImpl.class);
}
public static synchronized void useCommonsLogging() {
setImplementation(JakartaCommonsLoggingImpl.class);
}
public static synchronized void useLog4JLogging() {
setImplementation(Log4jImpl.class);
}
public static synchronized void useLog4J2Logging() {
setImplementation(Log4j2Impl.class);
}
public static synchronized void useJdkLogging() {
setImplementation(Jdk14LoggingImpl.class);
}
public static synchronized void useStdOutLogging() {
setImplementation(StdOutImpl.class);
}
public static synchronized void useNoLogging() {
setImplementation(NoLoggingImpl.class);
}
private static void tryImplementation(Runnable runnable) {
if(logConstructor == null) {
try {
//执行调用方法(也就是static "::"的调用)
runnable.run();
} catch (Throwable var2) {
;
}
}
}
// 获取实例
private static void setImplementation(Class extends Log> implClass) {
try {
Constructor extends Log> candidate = implClass.getConstructor(new Class[]{String.class});
Log log = (Log)candidate.newInstance(new Object[]{LogFactory.class.getName()});
if(log.isDebugEnabled()) {
log.debug("Logging initialized using '" + implClass + "' adapter.");
}
//获取到实例之后,把构造方法赋给logConstructor ,那么runnable.run(); 这行代码就不执行
logConstructor = candidate;
} catch (Throwable var3) {
throw new LogException("Error setting Log implementation. Cause: " + var3, var3);
}
}
// 依次去检测日志组件,"::"jdk1.8 新特性,相当于一个useSlf4jLogging的引用
static {
tryImplementation(LogFactory::useSlf4jLogging);
tryImplementation(LogFactory::useCommonsLogging);
tryImplementation(LogFactory::useLog4J2Logging);
tryImplementation(LogFactory::useLog4JLogging);
tryImplementation(LogFactory::useJdkLogging);
tryImplementation(LogFactory::useNoLogging);
}
}
最后调用 setImplementation() 方法获得了Slf4jImpl 实例,
2、代理类Slf4jImpl源码:通过if 判断去创建Slf4jLocationAwareLoggerImpl 实例或者 Slf4jLoggerImpl实例,
package org.apache.ibatis.logging.slf4j;
import org.apache.ibatis.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.spi.LocationAwareLogger;
public class Slf4jImpl implements Log {
private Log log;
public Slf4jImpl(String clazz) {
Logger logger = LoggerFactory.getLogger(clazz);
if(logger instanceof LocationAwareLogger) {
try {
logger.getClass().getMethod("log", new Class[]{Marker.class, String.class, Integer.TYPE, String.class, Object[].class, Throwable.class});
this.log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger)logger);
return;
} catch (SecurityException var4) {
;
} catch (NoSuchMethodException var5) {
;
}
}
this.log = new Slf4jLoggerImpl(logger);
}
public boolean isDebugEnabled() {
return this.log.isDebugEnabled();
}
public boolean isTraceEnabled() {
return this.log.isTraceEnabled();
}
public void error(String s, Throwable e) {
this.log.error(s, e);
}
public void error(String s) {
this.log.error(s);
}
public void debug(String s) {
this.log.debug(s);
}
public void trace(String s) {
this.log.trace(s);
}
public void warn(String s) {
this.log.warn(s);
}
}
3、适配器 Slf4jLocationAwareLoggerImpl 源码:把Slf4j 的Log适配成mybatis的Log
package org.apache.ibatis.logging.slf4j;
import org.apache.ibatis.logging.Log;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.spi.LocationAwareLogger;
class Slf4jLocationAwareLoggerImpl implements Log {
private static final Marker MARKER = MarkerFactory.getMarker("MYBATIS");
private static final String FQCN = Slf4jImpl.class.getName();
private final LocationAwareLogger logger;
Slf4jLocationAwareLoggerImpl(LocationAwareLogger logger) {
this.logger = logger;
}
public boolean isDebugEnabled() {
return this.logger.isDebugEnabled();
}
public boolean isTraceEnabled() {
return this.logger.isTraceEnabled();
}
public void error(String s, Throwable e) {
this.logger.log(MARKER, FQCN, 40, s, (Object[])null, e);
}
public void error(String s) {
this.logger.log(MARKER, FQCN, 40, s, (Object[])null, (Throwable)null);
}
public void debug(String s) {
this.logger.log(MARKER, FQCN, 10, s, (Object[])null, (Throwable)null);
}
public void trace(String s) {
this.logger.log(MARKER, FQCN, 0, s, (Object[])null, (Throwable)null);
}
public void warn(String s) {
this.logger.log(MARKER, FQCN, 30, s, (Object[])null, (Throwable)null);
}
}
适配器Slf4jLoggerImpl源码:
package org.apache.ibatis.logging.slf4j;
import org.apache.ibatis.logging.Log;
import org.slf4j.Logger;
class Slf4jLoggerImpl implements Log {
private final Logger log;
public Slf4jLoggerImpl(Logger logger) {
this.log = logger;
}
public boolean isDebugEnabled() {
return this.log.isDebugEnabled();
}
public boolean isTraceEnabled() {
return this.log.isTraceEnabled();
}
public void error(String s, Throwable e) {
this.log.error(s, e);
}
public void error(String s) {
this.log.error(s);
}
public void debug(String s) {
this.log.debug(s);
}
public void trace(String s) {
this.log.trace(s);
}
public void warn(String s) {
this.log.warn(s);
}
}
JDBC包是用来提供操作数据库的日志的:打印sql语句、参数、结果集等。
BaseJdbcLogger 是一个抽象类,InvocationHandler 是一个实现动态代理的接口。
ConnectionLogger:负责打印连接信息和SQL 语句,并创建PreparedStatementLogger;
PreparedStatementLogger:负责打印参数信 息,并创建ResultSetLogger
ResultSetLogge:负责打印数据结果信息