1、日志工厂
如果一个数据库操作,出现了异常,我们需要排错,日志是最好的助手
设置名 | 描述 | 有效值 | 默认值 |
---|---|---|---|
logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J | LOG4J(deprecated since 3.5.9) | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING | 未设置 |
- SLF4J
- LOG4J【掌握】
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING【掌握】
- NO_LOGGING
在Mybatis中具体使用哪一个日志实现,在设置中设定
2、STDOUT_LOGGING标准日志输出
mybatis-config.xml:
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
输出:
Created connection 1338841523.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4fcd19b3]
==> Preparing: select *from mybatis.user where id = ?
==> Parameters: 1(Integer)
<== Columns: id, name, pwd
<== Row: 1, zrk, 123
<== Total: 1
User{id=1, name='zrk', password='123'}
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4fcd19b3]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@4fcd19b3]
Returned connection 1338841523 to pool.
3.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/vekzjj.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
-
配置log4j为日志的实现
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
-
Log4j的使用
控制台输出:
Opening JDBC Connection Created connection 1095293768. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4148db48] ==> Preparing: select *from mybatis.user where id = ? ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, zrk, 123 <== Total: 1 User{id=1, name='zrk', password='123'} Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4148db48] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@4148db48] Returned connection 1095293768 to pool.
简单使用
-
在使用logj的类中导入 import org.apache.log4j.Logger;
-
日志对象,参数为当前类的class
static Logger logger = Logger.getLogger(String.valueOf(UserDaoTest.class));
-
测试
package com.vekzjj.dao; import com.vekzjj.pojo.User; import com.vekzjj.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import java.util.List; public class UserDaoTest { static Logger logger = Logger.getLogger(String.valueOf(UserDaoTest.class)); @Test public void test(){ //第一步:获得SqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //方式一:getMapper UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); sqlSession.close(); } @Test public void textLog4j(){ logger.info("info:进入了testLog4j"); logger.debug("debug:进入了testLog4j"); logger.error("error:进入了testLog4j"); } }
[INFO][22-04-14][class com.vekzjj.dao.UserDaoTest]info:进入了testLog4j [DEBUG][22-04-14][class com.vekzjj.dao.UserDaoTest]debug:进入了testLog4j [ERROR][22-04-14][class com.vekzjj.dao.UserDaoTest]error:进入了testLog4j