6.1 日志工厂
如果一个数据库操作,出现了异常,我们需要排错,日志就是最好的助手!
-
曾经:sout、debug
-
现在:日志工厂
- SLF4J
- LOG4J 【掌握】
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING 【掌握】
- NO_LOGGING
在MyBatis中具体使用哪一个日志实现,在设置中设定
STDOUT_LOGGING标准日志输出
在mybatis核心配置文件中,配置我们的日志!
手动配置日志
顺序不能出错
name和value不能有错误
<!--日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
输出结果
Opening JDBC Connection // 打开jdbc连接
Created connection 422330142. // 创建connection连接对象,本质还是jdbc
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 设置自动提交的事务为 false
==> Preparing: select * from user where id = ? // 预编译的sql
==> Parameters: 1(Integer) // sql传递的参数
<== Columns: id, name, pwd // 查询的列
<== Row: 1, 张三, 123 // 查询的记录
<== Total: 1 // 总共多少记录
User{id=1, name='张三', password='123'} // 结果
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 设置自动提交事务为 true
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 关闭JDBC连接
Returned connection 422330142 to pool. // 把connection返回池子
错误
ClassNotFoundException
-
包名/类名,写错了
-
没有导包
6.2 Log4j
什么是Log4j?
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件;
我们也可以控制每一条日志的输出格式;
通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程;
通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
配置
-
先导入log4j的包
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
-
通过一个配置文件来灵活地进行配置
log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 # 输出日志级别 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/rzp.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.sq1.PreparedStatement=DEBUG
-
配置settings为log4j实现
<settings> <setting name="logImpl" value="LOG4J"/> </settings>
-
测试运行
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. [org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. [org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VFS [org.apache.ibatis.io.JBoss6VFS]-JBoss 6 VFS API is not available in this environment. [org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VirtualFile [org.apache.ibatis.io.VFS]-VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment. [org.apache.ibatis.io.VFS]-Using VFS adapter org.apache.ibatis.io.DefaultVFS [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo [org.apache.ibatis.io.DefaultVFS]-Reader entry: User.class [org.apache.ibatis.io.DefaultVFS]-Listing file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo/User.class [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo/User.class [org.apache.ibatis.io.DefaultVFS]-Reader entry: 漱壕 4 < [org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.yin.pojo.User matches criteria [is assignable to Object] [org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections. [org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections. [org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections. [org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection [org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 255334292. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794] [com.yin.dao.UserMapper.getUserById]-==> Preparing: select * from user where id = ? [com.yin.dao.UserMapper.getUserById]-==> Parameters: 1(Integer) [com.yin.dao.UserMapper.getUserById]-<== Total: 1 User{id=1, name='张三', password='123'} [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794] [org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 255334292 to pool. Process finished with exit code 0
Log4j简单使用
-
在要使用Log4j的类中,导入包
import org.apache.log4j.Logger
-
日志对象,参数为当前类的class对象
static Logger logger = Logger.getLogger(UserDaoTest.class);
-
日志级别
@Test public void testLog4j(){ logger.info("info:进入了testLog4j"); //info:信息 logger.debug("debug:进入了testLog4j"); logger.error("error:进入了testLog4j"); } ================================================= [com.yin.dao.UserDaoTest]-info:进入了testLog4j [com.yin.dao.UserDaoTest]-debug:进入了testLog4j [com.yin.dao.UserDaoTest]-error:进入了testLog4j