Mybatis系列--07-日志相关
07-Mybatis系列-日志相关
概述
本文主要讲述Mybatis中调试日志的设置
日志工厂
MyBatis配置文件中settings
支持设置指定日志的具体实现,默认为空即无日志,官网链接
Mybatis支持的日志实现有以下这些
SLF4J | LOG4J(3.5.9 起废弃) | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING
STDOUT_LOGGING
实现
- 在mybatis-config.xml中
properties
和typeAlias
页签之前插入settings
设置
<properties resource="db.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<package name="com.kuang.pojo" />
</typeAliases>
- 直接运行测试代码即可打印出日志
运行结果
com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 dao.UserMapperTest,getList
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Class not found: org.jboss.vfs.VFS
JBoss 6 VFS API is not available in this environment.
Class not found: org.jboss.vfs.VirtualFile
VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
Using VFS adapter org.apache.ibatis.io.DefaultVFS
...
Checking to see if class com.kuang.pojo.User matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Created connection 251210093.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@ef9296d]
==> Preparing: select * from user
==> Parameters:
<== Columns: id, name, pwd
<== Row: 1, 狂神, 123456
<== Row: 2, 张三, 111
<== Row: 3, 李四, 222
<== Row: 4, haha, 123456
<== Row: 5, heihei, 123456
<== Total: 5
User{id=1, name='狂神', password='123456'}
User{id=2, name='张三', password='111'}
User{id=3, name='李四', password='222'}
User{id=4, name='haha', password='123456'}
User{id=5, name='heihei', password='123456'}
SLF4J
实现
狂神讲的是log4j,目前在Mybatis3.5.9的版本已经弃用了,log4j的替代版本即为logback, 所以我直接在官网上找了Slf4j的实现来看,Slf4j是一个日志接口,而Logback是它的一个实现
- 在项目pom.xml文件中引入logback依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
- 在mybatis-config.xml中
properties
和typeAlias
页签之前插入settings
设置
<settings>
<setting name="logImpl" value="SLF4J" />
</settings>
- 在resources目录下创建logback.xml
这里面仅有一些简单的配置,具体的配置需要在logback的官网查看
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%5level [%thread] - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.kuang.dao.UserMapper">
<level value="trace" />
</logger>
<root>
<appender-ref ref="stdout" />
</root>
</configuration>
运行结果
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
...
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
DEBUG [main] - Created connection 1313916817.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4e50c791]
DEBUG [main] - ==> Preparing: select * from user
DEBUG [main] - ==> Parameters:
TRACE [main] - <== Columns: id, name, pwd
TRACE [main] - <== Row: 1, 狂神, 123456
TRACE [main] - <== Row: 2, 张三, 111
TRACE [main] - <== Row: 3, 李四, 222
TRACE [main] - <== Row: 4, haha, 123456
TRACE [main] - <== Row: 5, heihei, 123456
DEBUG [main] - <== Total: 5
User{id=1, name='狂神', password='123456'}
User{id=2, name='张三', password='111'}
User{id=3, name='李四', password='222'}
User{id=4, name='haha', password='123456'}
User{id=5, name='heihei', password='123456'}