springboot下mybatis-plus如何打印sql日志和参数到日志文件

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

com.baomidoumybatis-plus-boot-starter3.2.0

【springboot下mybatis-plus如何打印sql日志和参数到日志文件】使用中需要打印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 activeJDBC Connection [HikariProxyConnection@6761653 wrapping oracle.jdbc.driver.T4CConnection@1ea226b] will not be managed by SpringJsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3WHERE name = ?==>Preparing: SELECT COUNT(1) FROM T_USER_TEST3 WHERE name = ? ==> Parameters: zhangsan3(String)<==Columns: COUNT(1)<==Row: 0Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15b67b]

但是项目打成jar包运行后,日志文件上并没有把sql日志记录下来,查看 spring.log文件只看到初始启动日志。
2021-07-22 16:43:26.429INFO 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.456INFO 35468 --- [main] c.dragonsoft.demojar.DemoJarApplication: Started DemoJarApplication in 21.168 seconds (JVM running for 22.947)2021-07-22 16:43:45.047INFO 35468 --- [http-nio-8080-exec-7] o.a.c.c.C.[Tomcat].[localhost].[/test]: Initializing Spring DispatcherServlet 'dispatcherServlet'2021-07-22 16:43:45.047INFO 35468 --- [http-nio-8080-exec-7] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2021-07-22 16:43:45.062INFO 35468 --- [http-nio-8080-exec-7] o.s.web.servlet.DispatcherServlet: Completed initialization in 14 ms

其实StdOutImpl只是把日志打印到控制台,看下StdOutImpl的源码所在的包,是用System.out.println打印的日志,显然这个是把日志打印到控制台,并不会存到日志文件。
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

我们知道springboot默认使用的是logback日志组件,mybatis-plus同时提供了slf4j的日志实现,这个正是我们要用的。
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

修改日志输出方式为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(),
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

StdOutImpl默认是返回true;
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

而slf4j,由于没有指定debug日志级别导致日志没有输出,
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

指定mybatisplus包名下的日志级别为debug。
logging.level.com.baomidou.mybatisplus=DEBUG

重启后再试发现日志成功打印出来了,
控制台:
2021-07-22 17:29:52.621INFO 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.642INFO 38796 --- [main] c.dragonsoft.demojar.DemoJarApplication: Started DemoJarApplication in 23.164 seconds (JVM running for 24.643)2021-07-22 17:29:58.715INFO 38796 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/test]: Initializing Spring DispatcherServlet 'dispatcherServlet'2021-07-22 17:29:58.716INFO 38796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2021-07-22 17:29:58.737INFO 38796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 21 ms2021-07-22 17:30:44.025WARN 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_TEST3WHERE name = ?

日志文件:
2021-07-22 17:29:52.621INFO 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.642INFO 38796 --- [main] c.dragonsoft.demojar.DemoJarApplication: Started DemoJarApplication in 23.164 seconds (JVM running for 24.643)2021-07-22 17:29:58.715INFO 38796 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/test]: Initializing Spring DispatcherServlet 'dispatcherServlet'2021-07-22 17:29:58.716INFO 38796 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2021-07-22 17:29:58.737INFO 38796 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 21 ms2021-07-22 17:30:44.025WARN 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_TEST3WHERE name = ?

但是只是打印了sql,参数呢?通过阅读源码发现参数打印是由logging目录下的这几个类实现的;
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

具体实现是通过动态代理的方式,
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

代理的初始化是在org.apache.ibatis.executor.BaseExecutor类进行,可以发现同样有一个debug判断,
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

查看log实现类,发现项目模块mapper目录没有指定日志级别
springboot下mybatis-plus如何打印sql日志和参数到日志文件
文章图片

修改项目mapper目录日志级别为debug后发现sql日志和参数成功打印。
logging.level.com.dragonsoft.demojar.mapper=DEBUG

控制台:
2021-07-22 17:46:29.255INFO 38688 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2021-07-22 17:46:29.268INFO 38688 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet: Completed initialization in 13 ms2021-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_TEST3WHERE 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.254INFO 38688 --- [http-nio-8080-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/test]: Initializing Spring DispatcherServlet 'dispatcherServlet'2021-07-22 17:46:29.255INFO 38688 --- [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2021-07-22 17:46:29.268INFO 38688 --- [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet: Completed initialization in 13 ms2021-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_TEST3WHERE 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=DEBUGmybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl

当然,生产环境下还是需要谨慎考虑一下是否开启sql日志,因为日志记录是会进行频繁的IO,会有一定的性能损耗。
到此这篇关于springboot下mybatis-plus如何打印sql日志和参数到日志文件的文章就介绍到这了,更多相关mybatis-plus 打印sql日志内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读