您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“springboot下mybatis-plus怎么打印sql日志和參數(shù)到日志文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springboot下mybatis-plus怎么打印sql日志和參數(shù)到日志文件”吧!
最近在使用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日志和參數(shù),網(wǎng)上很多的做法是增加日志輸出配置。
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
加上去后日志確實(shí)是出來,可以看到idea控制臺(tái)上將sql和參數(shù)打印出來了。
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]
但是項(xiàng)目打成jar包運(yùn)行后,日志文件上并沒有把sql日志記錄下來,查看 spring.log文件只看到初始啟動(dòng)日志。
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
其實(shí)StdOutImpl只是把日志打印到控制臺(tái),看下StdOutImpl的源碼所在的包,是用System.out.println打印的日志,顯然這個(gè)是把日志打印到控制臺(tái),并不會(huì)存到日志文件。
我們知道springboot默認(rèn)使用的是logback日志組件,mybatis-plus同時(shí)提供了slf4j的日志實(shí)現(xiàn),這個(gè)正是我們要用的。
修改日志輸出方式為slf4j,
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
重啟再試,日志依然沒打印出來,這次連控制臺(tái)也沒有輸出sql日志。
通過剛才的日志,我們看到打印sql的實(shí)現(xiàn)類是JsqlParserCountOptimize。
JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3 WHERE name = ?
跟蹤JsqlParserCountOptimize的代碼發(fā)現(xiàn),打印日志前有一個(gè)判斷l(xiāng)ogger.isDebugEnabled(),
StdOutImpl默認(rèn)是返回true;
而slf4j,由于沒有指定debug日志級(jí)別導(dǎo)致日志沒有輸出,
指定mybatisplus包名下的日志級(jí)別為debug。
logging.level.com.baomidou.mybatisplus=DEBUG
重啟后再試發(fā)現(xiàn)日志成功打印出來了,
控制臺(tái):
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,參數(shù)呢?通過閱讀源碼發(fā)現(xiàn)參數(shù)打印是由logging目錄下的這幾個(gè)類實(shí)現(xiàn)的;
具體實(shí)現(xiàn)是通過動(dòng)態(tài)代理的方式,
代理的初始化是在org.apache.ibatis.executor.BaseExecutor類進(jìn)行,可以發(fā)現(xiàn)同樣有一個(gè)debug判斷,
查看log實(shí)現(xiàn)類,發(fā)現(xiàn)項(xiàng)目模塊mapper目錄沒有指定日志級(jí)別
修改項(xiàng)目mapper目錄日志級(jí)別為debug后發(fā)現(xiàn)sql日志和參數(shù)成功打印。
logging.level.com.dragonsoft.demojar.mapper=DEBUG
控制臺(tái):
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)
總結(jié):總共需要以下三個(gè)配置
1:設(shè)置mybatisplus包下的日志級(jí)別為DEBUG;
2:設(shè)置項(xiàng)目mapper目錄的日志級(jí)別為DEBUG;
3:設(shè)置mybatis-plus的日志輸出方式為slf4j。
logging.level.com.baomidou.mybatisplus=DEBUG #項(xiàng)目mapper目錄 logging.level.com.dragonsoft.demojar.mapper=DEBUG mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
當(dāng)然,生產(chǎn)環(huán)境下還是需要謹(jǐn)慎考慮一下是否開啟sql日志,因?yàn)槿罩居涗浭菚?huì)進(jìn)行頻繁的IO,會(huì)有一定的性能損耗。
到此,相信大家對(duì)“springboot下mybatis-plus怎么打印sql日志和參數(shù)到日志文件”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。