Mybatis日志工厂(Log4j)

Mybatis日志工厂

在这里插入图片描述

STDOUT_LOGGING

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

日志输入如下

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
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Created connection 360207322.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@157853da]
==>  Preparing: select * from user; 
==> Parameters: 
<==    Columns: id, name, pwd
<==        Row: 1, sun, 123
<==        Row: 2, li, 123
<==        Row: 3, lisi, 123
<==      Total: 3
User(id=1, name=sun, password=123)
User(id=2, name=li, password=123)
User(id=3, name=lisi, password=123)
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@157853da]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@157853da]
Returned connection 360207322 to pool.

Process finished with exit code 0

Log4j

导入依赖

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

将mybatis是的日志实现方式改为log4j

<settings>
    <setting name="logImpl" value="LOG4J"/>
</settings>

log4j需要新建一个配置文件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/sun.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了

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页