MyBatis源码之日志模块

提供日志, 并提供集成第三方日志框架的能力, 具体参考类图

1. LogFactory

Log工厂类

1.1 构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Marker to be used by logging implementations that support markers.
*/
public static final String MARKER = "MYBATIS";

/**
* 使用的 Log 的构造方法
*/
private static Constructor<? extends Log> logConstructor;

static {
// 逐个尝试使用哪个实现类. 初始化logConstructor, 此处lambda
/**
* tryImplementation(new Runnable() {
* @Override
* public void run() {
* LogFactory.useSlf4jLogging();
* }
* });
*/
tryImplementation(LogFactory::useSlf4jLogging);
tryImplementation(LogFactory::useCommonsLogging);
tryImplementation(LogFactory::useLog4J2Logging);
tryImplementation(LogFactory::useLog4JLogging);
tryImplementation(LogFactory::useJdkLogging);
tryImplementation(LogFactory::useNoLogging);
}

private LogFactory() {
// disable construction
}

1.1.1 tryImplementation

1
2
3
4
5
6
7
8
9
private static void tryImplementation(Runnable runnable) {
if (logConstructor == null) {
try {
runnable.run();
} catch (Throwable t) {
// ignore
}
}
}

1.1.2 useSlf4jLogging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void useSlf4jLogging() {
setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
}

private static void setImplementation(Class<? extends Log> implClass) {
lock.lock();
try {
// 获得参数为 String 的构造方法
Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
// 创建Log对象 创建成功则赋值给 logConstructor
Log log = candidate.newInstance(LogFactory.class.getName());
if (log.isDebugEnabled()) {
log.debug("Logging initialized using '" + implClass + "' adapter.");
}
logConstructor = candidate;
} catch (Throwable t) {
throw new LogException("Error setting Log implementation. Cause: " + t, t);
} finally {
lock.unlock();
}
}

其它的Log对象与 Slf4j 类似, 如果需要自定义Log的实现类可以通过如下方法

1
2
3
public static void useCustomLogging(Class<? extends Log> clazz) {
setImplementation(clazz);
}

1.2 getLog

1
2
3
4
5
6
7
8
9
10
11
public static Log getLog(Class<?> clazz) {
return getLog(clazz.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);
}
}

2. Log

MyBatis 的日志父接口, 实现类较多 代码简单 以Slf4举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface Log {

boolean isDebugEnabled();

boolean isTraceEnabled();

void error(String s, Throwable e);

void error(String s);

void debug(String s);

void trace(String s);

void warn(String s);

}

2.1 Slf4jImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Slf4jImpl implements Log {

private Log log;

public Slf4jImpl(String clazz) {
// 使用 slf4j.LoggerFactory 获得Logger对象
Logger logger = LoggerFactory.getLogger(clazz);
// 判断是否是 slf4j.LocationAwareLogger 的类型
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 | 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);
}

}