SpringBoot集成P6spy實現(xiàn)自定義SQL日志打印
前言
在項目中對數(shù)據(jù)庫中進行操作調(diào)試的時候,最重要的一個功能就是SQL打印。
如果使用Mybatis-plus的話,他會自帶一個SQL打印的功能。雖然它可以打印,但我覺得還不夠優(yōu)雅,因為包含著很多我們不需要的信息,所以這篇文章實現(xiàn)一下在代碼中優(yōu)雅的打印SQL。
介紹
P6spy是什么?
P6Spy 是針對數(shù)據(jù)庫訪問操作的動態(tài)監(jiān)測框架(為開源項目,項目首頁:www.p6spy.com)它使得數(shù)據(jù)庫數(shù)據(jù)可無縫截取和操縱,而不必對現(xiàn)有應用程序的代碼作任何修改。P6Spy 分發(fā)包包括P6Log,它是一個可記錄任何 Java 應用程序的所有JDBC事務的應用程序。其配置完成使用時,可以進行數(shù)據(jù)訪問性能的監(jiān)測。
我們最需要的功能,查看sql語句,不是預編譯的帶問號的哦,而是真正的數(shù)據(jù)庫執(zhí)行的sql,更直觀,更簡單。
使用
1 導入相關(guān)依賴
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>${version:}</version>//目前最新版3.9.1 </dependency>
2 修改application.yml配置文件,只需要將連接和驅(qū)動進行修改
url: jdbc:p6spy:mysql://localhost:3306/db1_datasource driver-class-name: com.p6spy.engine.spy.P6SpyDriver
3 新增spy.properties文件
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory appender=com.p6spy.engine.spy.appender.StdoutLogger excludecategories=info,debug,result,batc,resultset deregisterdrivers=true dateformat=yyyy-MM-dd HH:mm:ss driverlist=com.mysql.cj.jdbc.Driver outagedetection=true outagedetectioninterval=2 #自定義SQL打印處理類 logMessageFormat=com.xxx.xxx.P6SpyLogger
4 新增P6SpyLogger,處理打印的SQL語句
import com.p6spy.engine.spy.appender.MessageFormattingStrategy; import java.text.SimpleDateFormat; import java.util.Date; public class P6SpyLogger implements MessageFormattingStrategy { ? ? private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); ? ? /** ? ? ?* 自定義sql日志打印 ? ? ?* ? ? ?* @param connectionId 連接標識 ? ? ?* @param now ? ? ? ? ?執(zhí)行時間 ? ? ?* @param elapsed ? ? ?執(zhí)行秒數(shù)ms ? ? ?* @param category ? ? statement ? ? ?* @param prepared ? ? 準備sql語句 ? ? ?* @param sql ? ? ? ? ?sql語句 ? ? ?* @param s4 ? ? ? ? ? 數(shù)據(jù)庫url連接 ? ? ?* @return {@link String} ? ? ?*/ ? ? @Override ? ? public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String s4) { ? ? ? ? System.out.println(); ? ? ? ? if (!"".equals(sql.trim())) { ? ? ? ? ? ? String sqlBegin = "============== SQL LOGGER BEGIN =============="; ? ? ? ? ? ? String sqlExecuteTime = "SQL 執(zhí)行時間 ? ? ? :" + this.format.format(new Date()) + "\n"; ? ? ? ? ? ? String elapsedStr = "SQL 執(zhí)行毫秒 ? ? ? :" + elapsed + "ms" + "\n"; ? ? ? ? ? ? String sqlPrint = "SQL 執(zhí)行語句 ? ? ? :" + sql; ? ? ? ? ? ? //String sqlPrint = !"".equals(sql.trim()) ? this.format.format(new Date()) + " | took " + elapsed + "ms | " + category + " | connection " + connectionId + "\n " + sql + ";" : ""; ? ? ? ? ? ? String sqlEnd = "============== ?SQL LOGGER END ?=============="; ? ? ? ? ? ? return sqlBegin + "\r\n" + sqlExecuteTime + elapsedStr + sqlPrint + "\r\n" + sqlEnd; ? ? ? ? } ? ? ? ? return ""; ? ? } }
5 查看SQL打印效果
============== SQL LOGGER BEGIN ==============
SQL 執(zhí)行時間 :2023-07-12 11:37:43:296
SQL 執(zhí)行毫秒 :5ms
SQL 執(zhí)行語句 :select * from db2_user
============== SQL LOGGER END ==============
============== SQL LOGGER BEGIN ==============
SQL 執(zhí)行時間 :2023-07-12 11:37:47:707
SQL 執(zhí)行毫秒 :1ms
SQL 執(zhí)行語句 :select * from db1_user where age = 18 order by create_time desc
============== SQL LOGGER END ==============
總結(jié)
可以根據(jù)方法的參數(shù)進行不同的搭配來實現(xiàn)優(yōu)雅的SQL打印
到此這篇關(guān)于SpringBoot集成P6spy實現(xiàn)自定義SQL日志打印的文章就介紹到這了,更多相關(guān)SpringBoot P6spy自定義SQL打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud OpenFeign基本介紹與實現(xiàn)示例
OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡通信的細節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡通信細節(jié)。所有遠程調(diào)用,都像調(diào)用本地方法一樣完成2023-02-02Java向數(shù)據(jù)庫中插入數(shù)據(jù)后獲取自增ID的常用方法
有時候因為新增的需求需要獲取剛剛新增的數(shù)據(jù)的自增的主鍵ID,下面這篇文章主要給大家介紹了關(guān)于Java向數(shù)據(jù)庫中插入數(shù)據(jù)后獲取自增ID的常用方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11EL調(diào)用Java方法_動力節(jié)點Java學院整理
簡單來說,我們在一個類中的某個方法,可以使用EL進行調(diào)用,這個能被EL表達式調(diào)用的方法稱之為EL函數(shù),但是這種方式必須滿足兩點要求,具體哪兩點,大家可以參考下本文2017-07-07全網(wǎng)最新springboot整合mybatis-plus的過程
在本文中,介紹了 MyBatis-Plus 的核心功能和使用方法,包括如何配置分頁插件、編寫分頁查詢代碼、使用各種 Wrapper 構(gòu)建復雜查詢條件等,通過這些內(nèi)容,相信你已經(jīng)對 MyBatis-Plus 有了更深入的了解,并能夠在實際項目中靈活應用這些功能,感興趣的朋友跟隨小編一起看看吧2025-02-02詳解Servlet3.0新特性(從注解配置到websocket編程)
Servlet3.0的出現(xiàn)是servlet史上最大的變革,其中的許多新特性大大的簡化了web應用的開發(fā),為廣大勞苦的程序員減輕了壓力,提高了web開發(fā)的效率。2017-04-04