日志工厂的种类
这里主要就是讲一下关于核心配置文件中的logImpl日志设置
- SLF4J
- LOG4J(deprecated since 3.5.9) 掌握
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING 掌握
- NO_LOGGING
STDOUT_LOGGING
这个其实就是标准日志输出
我们可以配置一下
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
运行一下我们的测试类
import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MybatisUtils;
import java.util.List;
public class UserMapper {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
}
日志输出
D:\JDK\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=55853:D:\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar;D:\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit5-rt.jar;D:\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit-rt.jar;C:\Users\29523\IdeaProjects\Mybaits\Mybatis-02\target\test-classes;C:\Users\29523\IdeaProjects\Mybaits\Mybatis-02\target\classes;D:\apache-maven-3.8.4\maven_repo\junit\junit\4.11\junit-4.11.jar;D:\apache-maven-3.8.4\maven_repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\apache-maven-3.8.4\maven_repo\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar;D:\apache-maven-3.8.4\maven_repo\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;D:\apache-maven-3.8.4\maven_repo\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 UserMapper,test
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
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,
Created connection 1520387953.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5a9f4771]
==> Preparing: select `id`,`name`,`password` from user where `id`=?;
==> Parameters: 1(Integer)
<== Columns: id, name, password
<== Row: 1, libo, 000000
<== Total: 1
User{id=1, name='libo', pwd='000000'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5a9f4771]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5a9f4771]
Returned connection 1520387953 to pool.
首先她说初始化使用了哪个类,然后什么强制关闭所有连接
接下来才是真正的日志
首先她说打开数据库
然后创建一个数据库连接对象
设置自动提交为false
然后预编译语句为什么
参数是什么
查询的字段
行信息
总共有几行
最终的结果
重新设置自动提交为true
关闭数据库连接
LOG4J
LOG4J是什么?
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等
我们可以控制每一条日志的输出格式
通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程
最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
如何使用LOG4J?
- 首先我们需要导入LOG4J依赖包
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- 在Mybatis核心配置文件中配置
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
- 在resources文件夹下创建一个配置文件log4j.properties并配置
### 配置根 ###
log4j.rootLogger = debug,console ,fileAppender
### 设置输出sql的级别,其中logger后面的内容全部为jar包中所包含的包名 ###
log4j.logger.org.apache=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug
### 配置输出到控制台 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
### 配置输出到文件 ###
log4j.appender.fileAppender = org.apache.log4j.FileAppender
log4j.appender.fileAppender.File = logs/log.log
log4j.appender.fileAppender.Append = true
log4j.appender.fileAppender.Threshold = DEBUG
log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 配置输出到文件,并且每天都创建一个文件 ###
log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRollingFile.File = logs/log.log
log4j.appender.dailyRollingFile.Append = true
log4j.appender.dailyRollingFile.Threshold = DEBUG
log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 配置输出到文件,且大小到达指定尺寸的时候产生一个新的文件 ###
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n