6.1 日志工厂
如果一个数据库操作出现了异常,我们需要定位问题,所以要写日志,方便后期debug。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--导入外部配置文件-->
<properties resource="db.properties"/>
<settings>
<!-- 配置日志实现 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.lu.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/${mysql.db}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- input your mapper here -->
<package name="com.lu.dao"/>
</mappers>
</configuration>
在 mybatis核心配置文件中,添加 settings logImpl
来制定对应的日志工厂。
- STDOUT_LOGGING
- log4j
6.2 log4j
og4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务 器、NT的事件记录器、UNIX Syslog守护进程等导入依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
添加配置文件
log4j.properties ```日志模式
log4j.rootLogger=DEBUG,console,file
控制台输出
log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
文件输出
log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/debug.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
日志级别
log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
<a name="O3gMs"></a>
### 启用log4j
```xml
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
查看日志
org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/Volumes/software/Project/java/mybatis-study/mybatis-05/target/classes/com/lu/dao/TodoMapper.xml
[org.apache.ibatis.io.DefaultVFS]-Reader entry: <?xml version="1.0" encoding="UTF-8"?>
[org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.lu.dao.TodoMapper matches criteria [is assignable to Object]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 2001223946.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
[com.lu.dao.TodoMapper.queryAll]-==> Preparing: select * from todo;
[com.lu.dao.TodoMapper.queryAll]-==> Parameters:
[com.lu.dao.TodoMapper.queryAll]-<== Total: 3
Todo(id=2, content=睡觉, status=0)
Todo(id=3, content=打豆豆, status=1)
Todo(id=4, content=打豆豆, status=1)
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 2001223946 to pool.
使用log4j
public class AppTest {
static Logger logger = Logger.getLogger(AppTest.class);
/**
* Rigorous Test :-)
*/
@Test
public void testLog4j() {
logger.info("info:日志信息");
logger.debug("debug:debug信息");
logger.error("err:错误信息");
}
}
查看日志
debug.log
[INFO][21-12-15][com.lu.AppTest]info:日志信息
[DEBUG][21-12-15][com.lu.AppTest]debug:debug信息
[ERROR][21-12-15][com.lu.AppTest]err:错误信息