1. 整体了解一下
引用一段话:
无论在开发测试环境中,还是在线上生产环境中,日志在整个系统中的地位都是非常重要的。良好的日志功能可以帮助开发人员和测试人员快速定位 Bug 代码,也可以帮助运维人员快速定位性能瓶颈等问题。目前的 Java 世界中存在很多优秀的日志框架,例如 Log4j、 Log4j2、Slf4j 等。
mybatis这么优秀的框架, 肯定也会提供比较比较详细的日志输出信息, 其主要的功能由日志模块提供, mybatis日志模块主要做两件事.
- 集成常用的第三方日志框架
- 用jdk代理的实现来打印sql执行的日志
那现在就让我们来啃一下日志模块相关的源码, 首选我们看一下logging包的结构
通过看包的结构, 我们发现, 大部分的代码主要对我们常用的日志框架做适配集成, 如我们最常用的slf4j, log4j和log4j2, 而jdbc包主要提供处理sql执行时的日志打印.
然后我们大概看下这些类的关系:
2. LogFactory类
org.apache.ibatis.logging.LogFactory
, Log的工厂类
2.1 类的构造方法
//支持标记的日志实现将使用的标记
public static final String MARKER = "MYBATIS";
//日志实现的构造器, 用于创建Log接口的对象
private static Constructor<? extends Log> logConstructor;
static {
//逐个尝试, 看那个日志实现能初始化logConstructor的就直接选那个
//初始化的时候会判断非空, 如果已经初始化了是不会重复初始化的
tryImplementation(LogFactory::useSlf4jLogging);
tryImplementation(LogFactory::useCommonsLogging);
tryImplementation(LogFactory::useLog4J2Logging);
tryImplementation(LogFactory::useLog4JLogging);
tryImplementation(LogFactory::useJdkLogging);
tryImplementation(LogFactory::useNoLogging);
}
private LogFactory() {
// disable construction
}
静态代码块会从上往下尝试将logConstructor变量初始化, 按照slf4j, commons-logging, log2j2, log4j, jdk-logging,no-logging的顺序尝试, 如果没有日志组件, 默认会用jdk的logging(jdk自带的, 不会少), 如果实在是jdk的logging都没有, 就不打日志
-
tryImplementation(Runnable runnable)
方法
private static void tryImplementation(Runnable runnable) {
//判断如果logConstructor是空的情况下, 执行runnable的
//这里保证了不会重复初始化
if (logConstructor == null) {
try {
runnable.run();
} catch (Throwable t) {
// 忽略异常
}
}
}
这里的Runnable是个函数接口, 可以接收jdk8的lamba表达式
-
useSlf4jLogging()
方法
public static synchronized void useSlf4jLogging() {
//调用setImplementation, 并传入slf4j的Log的实现类
setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
}
- 其他类同的方法, 这里就不多说了
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);
}
如果有自定义的日志实现, 调用useCustomLogging(Class<? extends Log> clazz)
来实现
-
setImplementation(Class<? extends Log> implClass)
方法
private static void setImplementation(Class<? extends Log> implClass) {
try {
//获取日志实现的构造方法, 构造方法带了一个字符串参数
Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
//尝试创建Log对象
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);
}
}
2.2 getLog(String logger)
方法
public static Log getLog(String logger) {
try {
//用构造器创建Log的实例返回
return logConstructor.newInstance(logger);
} catch (Throwable t) {
throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t);
}
}
2.3 getLog(Class<?> aClass)
方法
public static Log getLog(Class<?> aClass) {
//调用上面的```getLog(String logger)```
return getLog(aClass.getName());
}
3. Log接口
org.apache.ibatis.logging.Log
, mybatis所有的日志打印都是通过Log这接口来使用的. 接口很简单
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);
}
它的实现类比较多, 我们挑一个说一下, 其他类同
3.1 Slf4jImpl 类
public class Slf4jImpl implements Log {
private Log log;
public Slf4jImpl(String clazz) {
Logger logger = LoggerFactory.getLogger(clazz);
//如果logger 是 LocationAwareLogger
if (logger instanceof LocationAwareLogger) {
try {
// check for slf4j >= 1.6 method signature
//根据指定参数尝试获取log的方法, 如果不报错则可以创建Slf4jLocationAwareLoggerImpl
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) {
//如果创建不成功, 退回用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);
}
}
- 在构造方法里, 会根据不同的slf4j的版本来选择不同的实现接口
4 BaseJdbcLogger类
- 这个类是执行sql日志的基类, 通过它我们可以了解到mybatis是如何打印sql, 参数和返回值的
4.1 构造实现
/**
* 缓存所有set方法的名称
*/
protected static final Set<String> SET_METHODS;
/**
* 存所有执行sql的方法
*/
protected static final Set<String> EXECUTE_METHODS = new HashSet<>();
/**
* 保存sql的参数名和参数值
*/
private final Map<Object, Object> columnMap = new HashMap<>();
/**
* 保存sql的参数名
*/
private final List<Object> columnNames = new ArrayList<>();
/**
* 保存执行sql的参数值
*/
private final List<Object> columnValues = new ArrayList<>();
/**
* Log日志实例
*/
protected final Log statementLog;
/**
* 目前主要发现它是用于控制prefix的=号的长度, 默认是1
*/
protected final int queryStack;
//默认的构造器, 如果queryStack不传, 则默认给1
public BaseJdbcLogger(Log log, int queryStack) {
this.statementLog = log;
if (queryStack == 0) {
this.queryStack = 1;
} else {
this.queryStack = queryStack;
}
}
static {
//初始化所有set方法的名称, 只要PreparedStatement方法下, 以set开头, 且参数个数大于1的方法名
//这里的方法跟Statement的也一样
SET_METHODS = Arrays.stream(PreparedStatement.class.getDeclaredMethods())
.filter(method -> method.getName().startsWith("set"))
.filter(method -> method.getParameterCount() > 1)
.map(Method::getName)
.collect(Collectors.toSet());
//初始化 要执行sql的方法名
EXECUTE_METHODS.add("execute");
EXECUTE_METHODS.add("executeUpdate");
EXECUTE_METHODS.add("executeQuery");
EXECUTE_METHODS.add("addBatch");
}
- 这里主要做一些初始化操作
4.2 setColumn(Object key, Object value)
方法
- 主要用于缓存列名和列值
protected void setColumn(Object key, Object value) {
columnMap.put(key, value);
columnNames.add(key);
columnValues.add(value);
}
4.3 getParameterValueString()
方法
- 将所有参数值拼接成字符串, 除了null之外的其他值都加上类开信息, 如: null,1(Integer),abc(String)
protected String getParameterValueString() {
List<Object> typeList = new ArrayList<>(columnValues.size());
for (Object value : columnValues) {
//如果是null, 则加一个'null'字符串
if (value == null) {
typeList.add("null");
} else {
//非null, 则在值的后面加上类型
typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
}
}
//toString()的结果是"[null,1(Integer),abc(String)]"
final String parameters = typeList.toString();
//截掉[], 变成"null,1(Integer),abc(String)"
return parameters.substring(1, parameters.length() - 1);
}
4.3 objectValueString(Object value)
方法
- 如果值是数组类型, 返回[1,2]之类的值
protected String objectValueString(Object value) {
if (value instanceof Array) {
try {
//如果是数据组, 则用数组的打印方式toString
return ArrayUtil.toString(((Array) value).getArray());
} catch (SQLException e) {
return value.toString();
}
}
return value.toString();
}
4.4 objectValueString(Object value)
方法
- 主要用于清空缓存, 因为一个PreparedStatement对象可以执行多次sql
protected void clearColumnInfo() {
columnMap.clear();
columnNames.clear();
columnValues.clear();
}
4.5 removeBreakingWhitespace(String original)
方法
- 将original字符串中的" \t\n\r\f"这些字符替换成" "空串, 这里将sql处理成长长的一行
protected String removeBreakingWhitespace(String original) {
StringTokenizer whitespaceStripper = new StringTokenizer(original);
StringBuilder builder = new StringBuilder();
while (whitespaceStripper.hasMoreTokens()) {
builder.append(whitespaceStripper.nextToken());
builder.append(" ");
}
return builder.toString();
}
4.6 prefix(boolean isInput)
方法
-
isInput
这个控制着箭头的方向
/**
* 主要用于产生日志前缀, 如: ==>,<==
* - ==> Parameters: 527(Long)
* - <== Updates: 1
*/
private String prefix(boolean isInput) {
char[] buffer = new char[queryStack * 2 + 2];
Arrays.fill(buffer, '=');
//最后面加个空格, queryStack * 2 + 1 相当于 buffer.length - 1
buffer[queryStack * 2 + 1] = ' ';
if (isInput) {
buffer[queryStack * 2] = '>';
} else {
buffer[0] = '<';
}
return new String(buffer);
}
下图的箭头的来源就是这个方法生成的:
4.7 debug(String text, boolean input)
方法
-
input
这个参数代表是否输入 - 打日志前都加个前缀箭头代表输入或输出
-
trace(String text, boolean input)
方法类同, 这里就不展开了
protected void debug(String text, boolean input) {
if (statementLog.isDebugEnabled()) {
//打印参数前, 加个前缀
statementLog.debug(prefix(input) + text);
}
}
5 ConnectionLogger类
-
org.apache.ibatis.logging.jdbc.ConnectionLogger
是 BaseJdbcLogger 的实现类, 并且实现了InvocationHandler接口 - 这个类通过jdk代理的方式, 返回一个日志代理的Connection的代理对象
5.1 构造器
//实际的Connection
private final Connection connection;
//私有构造器, 给当前类使用
private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
//调用 BaseJdbcLogger 的构造器
super(statementLog, queryStack);
this.connection = conn;
}
5.2 invoke(Object proxy, Method method, Object[] params)
方法
- 这是代理对象调用的方法
- 这里在调用connetion的创建具体的Statement之后, 返回对应的日志代理类
public Object invoke(Object proxy, Method method, Object[] params)
throws Throwable {
try {
//忽略Object的方法, 不做处理, 直接调用
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
//执行 prepareStatement 方法前, 如果是debug则打印一下sql, param[0]是sql字符串
if ("prepareStatement".equals(method.getName())) {
if (isDebugEnabled()) {
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
}
//执行方法, 获得真正的PreparedStatement
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
//返回有日志代理的PreparedStatement
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
} else if ("prepareCall".equals(method.getName())) {
//执行 prepareCall 方法前, 如果是debug则打印一下sql, param[0]是sql字符串
if (isDebugEnabled()) {
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
}
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
//返回有日志代理的PreparedStatement
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
} else if ("createStatement".equals(method.getName())) {
//同上
Statement stmt = (Statement) method.invoke(connection, params);
stmt = StatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
} else {
return method.invoke(connection, params);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
5.3 newInstance(Connection conn, Log statementLog, int queryStack)
方法
- 根据当前的Connection, 返回日志打印的代理对象
public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
ClassLoader cl = Connection.class.getClassLoader();
return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler);
}
6 PreparedStatementLogger 类
org.apache.ibatis.logging.jdbc.PreparedStatementLogger
是PreparedStatement的日志代理封装类, 继成org.apache.ibatis.logging.jdbc.BaseJdbcLogger
类的代理实现
6.1 invoke(Object proxy, Method method, Object[] params)
方法
- jdk代理的主要执行方法
@Override
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
try {
//忽略Object的方法, 不处理, 直接执行
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
//如果准要调用执行sql的方法, 则打印一下参数
if (EXECUTE_METHODS.contains(method.getName())) {
if (isDebugEnabled()) {
debug("Parameters: " + getParameterValueString(), true);
}
//将相关缓存清空, PreparedStatement是可以多次执行sql的
clearColumnInfo();
//如果是执行查询方法, 且ResultSet不为null则返回ResultSet的日志代理对象
if ("executeQuery".equals(method.getName())) {
ResultSet rs = (ResultSet) method.invoke(statement, params);
return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
} else {
return method.invoke(statement, params);
}
} else if (SET_METHODS.contains(method.getName())) {
//如果是set相关法, 则表明是绑定sql的参数值, 缓存相关的参数和值, 对null做'null'的处理
if ("setNull".equals(method.getName())) {
setColumn(params[0], null);
} else {
setColumn(params[0], params[1]);
}
return method.invoke(statement, params);
} else if ("getResultSet".equals(method.getName())) {
//返回ResultSet的日志代理对象
ResultSet rs = (ResultSet) method.invoke(statement, params);
return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
} else if ("getUpdateCount".equals(method.getName())) {
//如果是查询更新条数, 则直接打印
int updateCount = (Integer) method.invoke(statement, params);
if (updateCount != -1) {
debug(" Updates: " + updateCount, false);
}
return updateCount;
} else {
return method.invoke(statement, params);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
6.2 newInstance(PreparedStatement stmt, Log statementLog, int queryStack)
方法
- 同上, 创建PreparedStatement的日志代理对象
7 StatementLogger类
org.apache.ibatis.logging.jdbc.StatementLogger
继承org.apache.ibatis.logging.jdbc.BaseJdbcLogger
是Statement的日志代理类
7.1 invoke(Object proxy, Method method, Object[] params)
方法
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
try {
//忽略Object对象的方法,
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
if (EXECUTE_METHODS.contains(method.getName())) {
if (isDebugEnabled()) {
//执行查询前, 打印一下sql
debug(" Executing: " + removeBreakingWhitespace((String) params[0]), true);
}
if ("executeQuery".equals(method.getName())) {
//如果是查询, 则返回ResultSet的日志代理对象
ResultSet rs = (ResultSet) method.invoke(statement, params);
return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
} else {
return method.invoke(statement, params);
}
} else if ("getResultSet".equals(method.getName())) {
//获取ResultSet时, 返回日志代理对象
ResultSet rs = (ResultSet) method.invoke(statement, params);
return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
} else {
return method.invoke(statement, params);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
7.2 Statement newInstance(Statement stmt, Log statementLog, int queryStack)
方法同上
8 ResultSetLogger类
org.apache.ibatis.logging.jdbc.ResultSetLogger
继承org.apache.ibatis.logging.jdbc.BaseJdbcLogger, 是ResultSet的日志代理类
8.1 构造方法
- static代码块主要对一些不可直接显示的类型进行初始化
/**
* 二进制类型或大对象类型列表
*/
private static final Set<Integer> BLOB_TYPES = new HashSet<>();
/**
* 是否第一行, 如果是则打印列名
*/
private boolean first = true;
/**
* 记录查询结果的条数
*/
private int rows;
//代理的ResultSet
private final ResultSet rs;
/**
* 记录二进制类型或大对象类型的索引, 用于打印具体列的值时用特殊字符代替
*/
private final Set<Integer> blobColumns = new HashSet<>();
static {
//初始化二进制类型或大对象类型列表
BLOB_TYPES.add(Types.BINARY);
BLOB_TYPES.add(Types.BLOB);
BLOB_TYPES.add(Types.CLOB);
BLOB_TYPES.add(Types.LONGNVARCHAR);
BLOB_TYPES.add(Types.LONGVARBINARY);
BLOB_TYPES.add(Types.LONGVARCHAR);
BLOB_TYPES.add(Types.NCLOB);
BLOB_TYPES.add(Types.VARBINARY);
}
private ResultSetLogger(ResultSet rs, Log statementLog, int queryStack) {
super(statementLog, queryStack);
this.rs = rs;
}
8.2 invoke(Object proxy, Method method, Object[] params)
方法
- 代理类的调用方法
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
try {
//忽略Object对象的方法
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
//先执行方法
Object o = method.invoke(rs, params);
//如果执行的是next()方法, 直根据结果处理
if ("next".equals(method.getName())) {
if ((Boolean) o) {
//如果next取到值, 行数加一
rows++;
//如果是trace级别的日志, 记录更加详细的日志
if (isTraceEnabled()) {
//获取元数据
ResultSetMetaData rsmd = rs.getMetaData();
//获取列数
final int columnCount = rsmd.getColumnCount();
if (first) {
//设置first为非第一行
first = false;
//第一行, 打印列名
printColumnHeaders(rsmd, columnCount);
}
//打印列的值
printColumnValues(columnCount);
}
} else {
//最后一次获取不到值时, 打印sql总条数
debug(" Total: " + rows, false);
}
}
//这行不知道想干嘛, 对当前ResultSet的日志没影响
clearColumnInfo();
return o;
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
8.3 printColumnHeaders(ResultSetMetaData rsmd, int columnCount)
方法
- 方法主要用逗号分割打印列名, 并且记录那些列是BLOB_TYPE类型
private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws SQLException {
StringJoiner row = new StringJoiner(", ", " Columns: ", "");
for (int i = 1; i <= columnCount; i++) {
//记录当前i列是否是BLOB_TYPE类型
if (BLOB_TYPES.contains(rsmd.getColumnType(i))) {
blobColumns.add(i);
}
//将列表放到字符串中
row.add(rsmd.getColumnLabel(i));
}
//打印
trace(row.toString(), false);
}
8.3 printColumnValues(int columnCount)
方法
private void printColumnValues(int columnCount) {
StringJoiner row = new StringJoiner(", ", " Row: ", "");
for (int i = 1; i <= columnCount; i++) {
try {
//BLOB_TYPE类型的值不能显示, 用<<BLOB>>来显示
if (blobColumns.contains(i)) {
row.add("<<BLOB>>");
} else {
row.add(rs.getString(i));
}
} catch (SQLException e) {
// generally can't call getString() on a BLOB column
row.add("<<Cannot Display>>");
}
}
trace(row.toString(), false);
}
8.4 newInstance(ResultSet rs, Log statementLog, int queryStack)
方法
- 同上, 创建代理对象