您好,登錄后才能下訂單哦!
本篇內容主要講解“springboot下mybatis-plus怎么打印sql日志和參數到日志文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“springboot下mybatis-plus怎么打印sql日志和參數到日志文件”吧!
最近在使用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,會有一定的性能損耗。
到此,相信大家對“springboot下mybatis-plus怎么打印sql日志和參數到日志文件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。