SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫
p6spy是一個開源的數(shù)據(jù)庫監(jiān)控插件,我們能通過使用p6spy打印數(shù)據(jù)庫操作或者保存操作日志
導(dǎo)入依賴
<!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.2</version>
</dependency>配置文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #原來 #spring.datasource.url=jdbc:mysql://x.x.x.x:3306/table #使用p6spy后需要修改成的樣子 spring.datasource.url=jdbc:p6spy:mysql://x.x.x.x:3306/table? spring.datasource.username=username spring.datasource.password=password #原來 #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #使用p6spy后需要修改成的樣子 spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
添加spy.properties配置文件(與application.yml同級)
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory # 使用日志系統(tǒng)記錄sql appender=com.p6spy.engine.spy.appender.Slf4JLogger ## 配置記錄Log例外 excludecategories=info,debug,result,batc,resultset # 設(shè)置使用p6spy driver來做代理 deregisterdrivers=true # 日期格式 dateformat=yyyy-MM-dd HH:mm:ss # 實際驅(qū)動 driverlist=com.mysql.cj.jdbc.Driver # 是否開啟慢SQL記錄 outagedetection=true # 慢SQL記錄標(biāo)準(zhǔn) 秒 outagedetectioninterval=2
這里我們能看到driverlist=com.mysql.cj.jdbc.Driver,結(jié)合上面2中修改數(shù)據(jù)庫驅(qū)動,能看出p6spy實際上是類似一種代理數(shù)據(jù)庫驅(qū)動,在我們訪問數(shù)據(jù)庫時會通過p6spy的驅(qū)動,然后p6spy再會去調(diào)用實際的數(shù)據(jù)庫驅(qū)動,這樣p6spy就能截獲數(shù)據(jù)庫操作了。
自定義日志打印
方式一:實現(xiàn)MessageFormattingStrategy接口
public class P6SpyLogger implements MessageFormattingStrategy {
/**
*
* @param connectionId 連接id
* @param now 當(dāng)前時間
* @param elapsed 耗時
* @param category 類型
* @param prepared 聲明的sql語句
* @param sql 有效的sql語句
* @param datasource 連接屬性
* @return
*/
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql,String datasource) {
return !"".equals(sql.trim()) ? "[ " + LocalDateTime.now() + " ] --- | took "
+ elapsed + "ms | " + category + " | connection " + connectionId +"\n " + "datasource " + datasource +"\n "
+ sql + ";" : "";
}
}然后在spy.properties中添加配置
# 自定義日志打印(就是上面添加的P6SpyLogger.java) #logMessageFormat=com.p6spy.demop6spy.conf.P6SpyLogger
方式二:使用p6spy提供的logMessageFormat實現(xiàn)
這里有兩種,都是在spy.properties中的配置
(1)com.p6spy.engine.spy.appender.SingleLineFormat這個就相當(dāng)于把所有信息在單行里全部顯示出來。
logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
(2)com.p6spy.engine.spy.appender.CustomLineFormat這個和(1)比起來就比較人性化,可以自定義
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=%(currentTime) | SQL use time: %(executionTime) ms | connect info: %(category)-%(connectionId) | execute sql: %(sql)
順便放一下spy.properties的配置詳細(xì)說明吧
# 指定應(yīng)用的日志攔截模塊,默認(rèn)為com.p6spy.engine.spy.P6SpyFactory #modulelist=com.p6spy.engine.spy.P6SpyFactory,com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory # 真實JDBC driver , 多個以 逗號 分割 默認(rèn)為空 #driverlist= # 是否自動刷新 默認(rèn) flase #autoflush=false # 配置SimpleDateFormat日期格式 默認(rèn)為空 #dateformat= # 打印堆棧跟蹤信息 默認(rèn)flase #stacktrace=false # 如果 stacktrace=true,則可以指定具體的類名來進(jìn)行過濾。 #stacktraceclass= # 監(jiān)測屬性配置文件是否進(jìn)行重新加載 #reloadproperties=false # 屬性配置文件重新加載的時間間隔,單位:秒 默認(rèn)60s #reloadpropertiesinterval=60 # 指定 Log 的 appender,取值: #appender=com.p6spy.engine.spy.appender.Slf4JLogger #appender=com.p6spy.engine.spy.appender.StdoutLogger #appender=com.p6spy.engine.spy.appender.FileLogger # 指定 Log 的文件名 默認(rèn) spy.log #logfile=spy.log # 指定是否每次是增加 Log,設(shè)置為 false 則每次都會先進(jìn)行清空 默認(rèn)true #append=true # 指定日志輸出樣式 默認(rèn)為com.p6spy.engine.spy.appender.SingleLineFormat , 單行輸出 不格式化語句 #logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat # 也可以采用 com.p6spy.engine.spy.appender.CustomLineFormat 來自定義輸出樣式, 默認(rèn)值是%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine) # 可用的變量為: # %(connectionId) connection id # %(currentTime) 當(dāng)前時間 # %(executionTime) 執(zhí)行耗時 # %(category) 執(zhí)行分組 # %(effectiveSql) 提交的SQL 換行 # %(effectiveSqlSingleLine) 提交的SQL 不換行顯示 # %(sql) 執(zhí)行的真實SQL語句,已替換占位 # %(sqlSingleLine) 執(zhí)行的真實SQL語句,已替換占位 不換行顯示 #customLogMessageFormat=%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine) # date類型字段記錄日志時使用的日期格式 默認(rèn)dd-MMM-yy #databaseDialectDateFormat=dd-MMM-yy # boolean類型字段記錄日志時使用的日期格式 默認(rèn)boolean 可選值numeric #databaseDialectBooleanFormat=boolean # 是否通過jmx暴露屬性 默認(rèn)true #jmx=true # 如果jmx設(shè)置為true 指定通過jmx暴露屬性時的前綴 默認(rèn)為空 # com.p6spy(.<jmxPrefix>)?:name=<optionsClassName> #jmxPrefix= # 是否顯示納秒 默認(rèn)false #useNanoTime=false # 實際數(shù)據(jù)源 JNDI #realdatasource=/RealMySqlDS # 實際數(shù)據(jù)源 datasource class #realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource # 實際數(shù)據(jù)源所攜帶的配置參數(shù) 以 k=v 方式指定 以 分號 分割 #realdatasourceproperties=port;3306,serverName;myhost,databaseName;jbossdb,foo;bar # jndi數(shù)據(jù)源配置 # 設(shè)置 JNDI 數(shù)據(jù)源的 NamingContextFactory。 #jndicontextfactory=org.jnp.interfaces.NamingContextFactory # 設(shè)置 JNDI 數(shù)據(jù)源的提供者的 URL。 #jndicontextproviderurl=localhost:1099 # 設(shè)置 JNDI 數(shù)據(jù)源的一些定制信息,以分號分隔。 #jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.naming:org.jnp.interfaces # 是否開啟日志過濾 默認(rèn)false, 這項配置是否生效前提是配置了 include/exclude/sqlexpression #filter=false # 過濾 Log 時所包含的表名列表,以逗號分隔 默認(rèn)為空 #include= # 過濾 Log 時所排除的表名列表,以逗號分隔 默認(rèn)為空 #exclude= # 過濾 Log 時的 SQL 正則表達(dá)式名稱 默認(rèn)為空 #sqlexpression= #顯示指定過濾 Log 時排隊的分類列表,取值: error, info, batch, debug, statement, #commit, rollback, result and resultset are valid values # (默認(rèn) info,debug,result,resultset,batch) #excludecategories=info,debug,result,resultset,batch # 是否過濾二進(jìn)制字段 # (default is false) #excludebinary=false # P6Log 模塊執(zhí)行時間設(shè)置,整數(shù)值 (以毫秒為單位),只有當(dāng)超過這個時間才進(jìn)行記錄 Log。 默認(rèn)為0 #executionThreshold= # P6Outage 模塊是否記錄較長時間運行的語句 默認(rèn)false # outagedetection=true|false # P6Outage 模塊執(zhí)行時間設(shè)置,整數(shù)值 (以秒為單位)),只有當(dāng)超過這個時間才進(jìn)行記錄 Log。 默認(rèn)30s # outagedetectioninterval=integer time (seconds)
總體項目結(jié)構(gòu)

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot實現(xiàn)通用Auth認(rèn)證的幾種方式
本文主要介紹了Springboot實現(xiàn)通用Auth認(rèn)證的幾種方式,主要介紹了4種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
Docker 解決openjdk容器里無法使用JDK的jmap等命令問題
這篇文章主要介紹了Docker 解決openjdk容器里無法使用JDK的jmap等命令問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
java用list集合存儲學(xué)生信息并算出成績平均值操作
這篇文章主要介紹了java用list集合存儲學(xué)生信息并算出成績平均值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
創(chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)
下面小編就為大家?guī)硪黄獎?chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Java實現(xiàn)樹形結(jié)構(gòu)管理的組合設(shè)計模式
Java組合模式是一種結(jié)構(gòu)型設(shè)計模式,它允許將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得用戶可以使用統(tǒng)一的方式處理單個對象和對象組合,從而簡化了系統(tǒng)的設(shè)計和維護(hù)2023-04-04

