5.日志
5.1日志工厂
数据库中如果出现异常,需要通过日志对其进行问题排查。
Mybatis 通过使用内置的日志工厂提供日志功能。内置日志工厂将会把日志工作委托给下面的实现之一:
- SLF4J
- Apache Commons Logging
- Log4j 2
- Log4j
- JDK logging
在mybatis配置文件中加入日志设置。
<settings>
<!--标准的日志工厂实现-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
5.2LOG4j
操作步骤:
1添加 Log4J 的 jar 包
由于我们使用的是 Log4J,我们要确保它的 jar 包可以被应用使用。为此,需要将 jar 包添加到应用的类路径中。Log4J 的 jar 包可以在上面的链接中下载。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2配置 Log4J
log4j.properties:
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
#控制台输出的相关设置
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
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=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
#文件输出的相关设置
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=./log/axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
3.加载log4j
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
4.log4j的使用,在测试案例中输出体现
2021-01-05 13:10:25,977 113 [ main] DEBUG ache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2021-01-05 13:10:25,998 134 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-05 13:10:25,998 134 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-05 13:10:25,998 134 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-05 13:10:25,999 135 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-05 13:10:26,153 289 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-01-05 13:10:26,441 577 [ main] DEBUG source.pooled.PooledDataSource - Created connection 603856241.
2021-01-05 13:10:26,441 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@23fe1d71]
2021-01-05 13:10:26,443 579 [ main] DEBUG .Z.dao.IUserMapper.getUserList - ==> Preparing: select * from user
2021-01-05 13:10:26,481 617 [ main] DEBUG .Z.dao.IUserMapper.getUserList - ==> Parameters:
2021-01-05 13:10:26,497 633 [ main] DEBUG .Z.dao.IUserMapper.getUserList - <== Total: 3
User{id=1, username='张三', birthday=null, sex='男', address='北京市西城区'}
User{id=46, username='小王', birthday=Mon Jun 18 00:00:00 CST 2018, sex='男', address='北京市顺义区'}
User{id=47, username='小儿王', birthday=Mon Aug 08 00:00:00 CST 2016, sex='女', address='北京市海淀区'}
2021-01-05 13:10:26,499 635 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@23fe1d71]
2021-01-05 13:10:26,500 636 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@23fe1d71]
2021-01-05 13:10:26,500 636 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 603856241 to pool.
日志级别
logger.info("info:进入了testLog4j");
logger.debug("debug:进入了testLog4j");
logger.error("error:进入了testLog4j");