亲宝软件园·资讯

展开

mybatis-plus打印sql日志和参数到日志文件

Joker2018 人气:46

最近在使用springboot过程中用到了mybatis-plus ,springboot版本是2.3.1.RELEASE,mybatis-plus 版本3.2.0。

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

使用中需要打印sql日志和参数,网上很多的做法是增加日志输出配置。

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

加上去后日志确实是出来,可以看到idea控制台上将sql和参数打印出来了。

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15b67b] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@6761653 wrapping oracle.jdbc.driver.T4CConnection@1ea226b] will not be managed by Spring
JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?
==>  Preparing: SELECT COUNT(1) FROM T_USER_TEST3 WHERE name = ? 
==> Parameters: zhangsan3(String)
<==    Columns: COUNT(1)
<==        Row: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15b67b]

但是项目打成jar包运行后,日志文件上并没有把sql日志记录下来,查看 spring.log文件只看到初始启动日志。

2021-07-22 16:43:26.429  INFO 35468 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/test'
2021-07-22 16:43:26.456  INFO 35468 --- [main] c.dragonsoft.demojar.DemoJarApplication  : Started DemoJarApplication in 21.168 seconds (JVM running for 22.947)
2021-07-22 16:43:45.047  INFO 35468 --- [http-nio-8080-exec-7] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-22 16:43:45.047  INFO 35468 --- [http-nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-22 16:43:45.062  INFO 35468 --- [http-nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms

其实StdOutImpl只是把日志打印到控制台,看下StdOutImpl的源码所在的包,是用System.out.println打印的日志,显然这个是把日志打印到控制台,并不会存到日志文件。

我们知道springboot默认使用的是logback日志组件,mybatis-plus同时提供了slf4j的日志实现,这个正是我们要用的。

 修改日志输出方式为slf4j,

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl

重启再试,日志依然没打印出来,这次连控制台也没有输出sql日志。

通过刚才的日志,我们看到打印sql的实现类是JsqlParserCountOptimize。

JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3 WHERE name = ?

跟踪JsqlParserCountOptimize的代码发现,打印日志前有一个判断logger.isDebugEnabled(),

StdOutImpl默认是返回true;

而slf4j,由于没有指定debug日志级别导致日志没有输出,

 指定mybatisplus包名下的日志级别为debug。

logging.level.com.baomidou.mybatisplus=DEBUG

重启后再试发现日志成功打印出来了,

 控制台:

2021-07-22 17:29:52.621  INFO 38796 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/test'
2021-07-22 17:29:52.642  INFO 38796 --- [           main] c.dragonsoft.demojar.DemoJarApplication  : Started DemoJarApplication in 23.164 seconds (JVM running for 24.643)
2021-07-22 17:29:58.715  INFO 38796 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-22 17:29:58.716  INFO 38796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-22 17:29:58.737  INFO 38796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 21 ms
2021-07-22 17:30:44.025  WARN 38796 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=1m521ms831µs400ns).
2021-07-22 17:30:45.131 DEBUG 38796 --- [nio-8080-exec-1] c.b.m.e.p.p.o.JsqlParserCountOptimize    : JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?

 日志文件:

2021-07-22 17:29:52.621  INFO 38796 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/test'
2021-07-22 17:29:52.642  INFO 38796 --- [main] c.dragonsoft.demojar.DemoJarApplication  : Started DemoJarApplication in 23.164 seconds (JVM running for 24.643)
2021-07-22 17:29:58.715  INFO 38796 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-22 17:29:58.716  INFO 38796 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-22 17:29:58.737  INFO 38796 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 21 ms
2021-07-22 17:30:44.025  WARN 38796 --- [HikariPool-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=1m521ms831µs400ns).
2021-07-22 17:30:45.131 DEBUG 38796 --- [http-nio-8080-exec-1] c.b.m.e.p.p.o.JsqlParserCountOptimize    : JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?

但是只是打印了sql,参数呢?通过阅读源码发现参数打印是由logging目录下的这几个类实现的;

具体实现是通过动态代理的方式,

代理的初始化是在org.apache.ibatis.executor.BaseExecutor类进行,可以发现同样有一个debug判断,

查看log实现类,发现项目模块mapper目录没有指定日志级别

 修改项目mapper目录日志级别为debug后发现sql日志和参数成功打印。

logging.level.com.dragonsoft.demojar.mapper=DEBUG

控制台:

2021-07-22 17:46:29.255  INFO 38688 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-22 17:46:29.268  INFO 38688 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms
2021-07-22 17:46:36.869 DEBUG 38688 --- [nio-8080-exec-6] c.b.m.e.p.p.o.JsqlParserCountOptimize    : JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?
2021-07-22 17:46:36.906 DEBUG 38688 --- [nio-8080-exec-6] c.d.d.m.U.pagingBySearchable             : ==>  Preparing: SELECT COUNT(1) FROM T_USER_TEST3 WHERE name = ? 
2021-07-22 17:46:36.998 DEBUG 38688 --- [nio-8080-exec-6] c.d.d.m.U.pagingBySearchable             : ==> Parameters: zhangsan3(String)

日志文件:

2021-07-22 17:46:29.254  INFO 38688 --- [http-nio-8080-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-22 17:46:29.255  INFO 38688 --- [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-22 17:46:29.268  INFO 38688 --- [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms
2021-07-22 17:46:36.869 DEBUG 38688 --- [http-nio-8080-exec-6] c.b.m.e.p.p.o.JsqlParserCountOptimize    : JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?
2021-07-22 17:46:36.906 DEBUG 38688 --- [http-nio-8080-exec-6] c.d.d.m.U.pagingBySearchable             : ==>  Preparing: SELECT COUNT(1) FROM T_USER_TEST3 WHERE name = ? 
2021-07-22 17:46:36.998 DEBUG 38688 --- [http-nio-8080-exec-6] c.d.d.m.U.pagingBySearchable             : ==> Parameters: zhangsan3(String)

总结:总共需要以下三个配置

1:设置mybatisplus包下的日志级别为DEBUG;
2:设置项目mapper目录的日志级别为DEBUG;
3:设置mybatis-plus的日志输出方式为slf4j。

logging.level.com.baomidou.mybatisplus=DEBUG
#项目mapper目录
logging.level.com.dragonsoft.demojar.mapper=DEBUG
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl

当然,生产环境下还是需要谨慎考虑一下是否开启sql日志,因为日志记录是会进行频繁的IO,会有一定的性能损耗。

加载全部内容

相关教程
猜你喜欢
用户评论