SpringBoot如何使用p6spy監(jiān)控?cái)?shù)據(jù)庫(kù)
p6spy是一個(gè)開(kāi)源的數(shù)據(jù)庫(kù)監(jiān)控插件,我們能通過(guò)使用p6spy打印數(shù)據(jù)庫(kù)操作或者保存操作日志
導(dǎo)入依賴(lài)
<!-- 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 #原來(lái) #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 #原來(lái) #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同級(jí))
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來(lái)做代理 deregisterdrivers=true # 日期格式 dateformat=yyyy-MM-dd HH:mm:ss # 實(shí)際驅(qū)動(dòng) driverlist=com.mysql.cj.jdbc.Driver # 是否開(kāi)啟慢SQL記錄 outagedetection=true # 慢SQL記錄標(biāo)準(zhǔn) 秒 outagedetectioninterval=2
這里我們能看到driverlist=com.mysql.cj.jdbc.Driver
,結(jié)合上面2中修改數(shù)據(jù)庫(kù)驅(qū)動(dòng),能看出p6spy實(shí)際上是類(lèi)似一種代理數(shù)據(jù)庫(kù)驅(qū)動(dòng),在我們?cè)L問(wèn)數(shù)據(jù)庫(kù)時(shí)會(huì)通過(guò)p6spy的驅(qū)動(dòng),然后p6spy再會(huì)去調(diào)用實(shí)際的數(shù)據(jù)庫(kù)驅(qū)動(dòng),這樣p6spy就能截獲數(shù)據(jù)庫(kù)操作了。
自定義日志打印
方式一:實(shí)現(xiàn)MessageFormattingStrategy接口
public class P6SpyLogger implements MessageFormattingStrategy { /** * * @param connectionId 連接id * @param now 當(dāng)前時(shí)間 * @param elapsed 耗時(shí) * @param category 類(lèi)型 * @param prepared 聲明的sql語(yǔ)句 * @param sql 有效的sql語(yǔ)句 * @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實(shí)現(xiàn)
這里有兩種,都是在spy.properties中的配置
(1)com.p6spy.engine.spy.appender.SingleLineFormat
這個(gè)就相當(dāng)于把所有信息在單行里全部顯示出來(lái)。
logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
(2)com.p6spy.engine.spy.appender.CustomLineFormat
這個(gè)和(1)比起來(lái)就比較人性化,可以自定義
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=%(currentTime) | SQL use time: %(executionTime) ms | connect info: %(category)-%(connectionId) | execute sql: %(sql)
順便放一下spy.properties的配置詳細(xì)說(shuō)明吧
# 指定應(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 # 真實(shí)JDBC driver , 多個(gè)以 逗號(hào) 分割 默認(rèn)為空 #driverlist= # 是否自動(dòng)刷新 默認(rèn) flase #autoflush=false # 配置SimpleDateFormat日期格式 默認(rèn)為空 #dateformat= # 打印堆棧跟蹤信息 默認(rèn)flase #stacktrace=false # 如果 stacktrace=true,則可以指定具體的類(lèi)名來(lái)進(jìn)行過(guò)濾。 #stacktraceclass= # 監(jiān)測(cè)屬性配置文件是否進(jìn)行重新加載 #reloadproperties=false # 屬性配置文件重新加載的時(shí)間間隔,單位:秒 默認(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 則每次都會(huì)先進(jìn)行清空 默認(rèn)true #append=true # 指定日志輸出樣式 默認(rèn)為com.p6spy.engine.spy.appender.SingleLineFormat , 單行輸出 不格式化語(yǔ)句 #logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat # 也可以采用 com.p6spy.engine.spy.appender.CustomLineFormat 來(lái)自定義輸出樣式, 默認(rèn)值是%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine) # 可用的變量為: # %(connectionId) connection id # %(currentTime) 當(dāng)前時(shí)間 # %(executionTime) 執(zhí)行耗時(shí) # %(category) 執(zhí)行分組 # %(effectiveSql) 提交的SQL 換行 # %(effectiveSqlSingleLine) 提交的SQL 不換行顯示 # %(sql) 執(zhí)行的真實(shí)SQL語(yǔ)句,已替換占位 # %(sqlSingleLine) 執(zhí)行的真實(shí)SQL語(yǔ)句,已替換占位 不換行顯示 #customLogMessageFormat=%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine) # date類(lèi)型字段記錄日志時(shí)使用的日期格式 默認(rèn)dd-MMM-yy #databaseDialectDateFormat=dd-MMM-yy # boolean類(lèi)型字段記錄日志時(shí)使用的日期格式 默認(rèn)boolean 可選值numeric #databaseDialectBooleanFormat=boolean # 是否通過(guò)jmx暴露屬性 默認(rèn)true #jmx=true # 如果jmx設(shè)置為true 指定通過(guò)jmx暴露屬性時(shí)的前綴 默認(rèn)為空 # com.p6spy(.<jmxPrefix>)?:name=<optionsClassName> #jmxPrefix= # 是否顯示納秒 默認(rèn)false #useNanoTime=false # 實(shí)際數(shù)據(jù)源 JNDI #realdatasource=/RealMySqlDS # 實(shí)際數(shù)據(jù)源 datasource class #realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource # 實(shí)際數(shù)據(jù)源所攜帶的配置參數(shù) 以 k=v 方式指定 以 分號(hào) 分割 #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ù)源的一些定制信息,以分號(hào)分隔。 #jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.naming:org.jnp.interfaces # 是否開(kāi)啟日志過(guò)濾 默認(rèn)false, 這項(xiàng)配置是否生效前提是配置了 include/exclude/sqlexpression #filter=false # 過(guò)濾 Log 時(shí)所包含的表名列表,以逗號(hào)分隔 默認(rèn)為空 #include= # 過(guò)濾 Log 時(shí)所排除的表名列表,以逗號(hào)分隔 默認(rèn)為空 #exclude= # 過(guò)濾 Log 時(shí)的 SQL 正則表達(dá)式名稱(chēng) 默認(rèn)為空 #sqlexpression= #顯示指定過(guò)濾 Log 時(shí)排隊(duì)的分類(lèi)列表,取值: 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 # 是否過(guò)濾二進(jìn)制字段 # (default is false) #excludebinary=false # P6Log 模塊執(zhí)行時(shí)間設(shè)置,整數(shù)值 (以毫秒為單位),只有當(dāng)超過(guò)這個(gè)時(shí)間才進(jìn)行記錄 Log。 默認(rèn)為0 #executionThreshold= # P6Outage 模塊是否記錄較長(zhǎng)時(shí)間運(yùn)行的語(yǔ)句 默認(rèn)false # outagedetection=true|false # P6Outage 模塊執(zhí)行時(shí)間設(shè)置,整數(shù)值 (以秒為單位)),只有當(dāng)超過(guò)這個(gè)時(shí)間才進(jìn)行記錄 Log。 默認(rèn)30s # outagedetectioninterval=integer time (seconds)
總體項(xiàng)目結(jié)構(gòu)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫(kù)的步驟全紀(jì)錄
這篇文章主要給大家介紹了關(guān)于Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Java運(yùn)行時(shí)動(dòng)態(tài)生成對(duì)象的方法小結(jié)
Java是一門(mén)靜態(tài)語(yǔ)言,通常,我們需要的class在編譯的時(shí)候就已經(jīng)生成了,為什么有時(shí)候我們還想在運(yùn)行時(shí)動(dòng)態(tài)生成class呢?今天通過(guò)本文給大家分享Java運(yùn)行時(shí)動(dòng)態(tài)生成對(duì)象的方法小結(jié),需要的朋友參考下吧2021-08-08使用springboot訪問(wèn)圖片本地路徑并映射成url
這篇文章主要介紹了使用springboot訪問(wèn)圖片本地路徑并映射成url的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08IDEA中用maven連接數(shù)據(jù)庫(kù)的教程
這篇文章主要介紹了IDEA中用maven連接數(shù)據(jù)庫(kù)的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11List集合中對(duì)數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例
今天小編就為大家分享一篇關(guān)于List集合中對(duì)數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12