溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot結合P6Spy的實例分析

發(fā)布時間:2021-12-04 09:56:22 來源:億速云 閱讀:164 作者:柒染 欄目:軟件技術

這篇文章給大家介紹Spring Boot結合P6Spy的實例分析,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一:pom.xml 中添加依賴

		<dependency>
			<groupId>p6spy</groupId>
			<artifactId>p6spy</artifactId>
			<version>3.8.0</version>
		</dependency>

二:application.xml 修改數(shù)據(jù)源配置

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:p6spy:mysql://...
    username: ...
    password: ...
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver

三:配置文件spy.properties

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定義日志打印
logMessageFormat=org.pkaq.core.log.P6SpyLogger
# 使用日志系統(tǒng)記錄sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置記錄Log例外
excludecategories=info,debug,result,batc,resultset
# 設置使用p6spy driver來做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實際驅(qū)動 
driverlist=com.mysql.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標準 秒
outagedetectioninterval=2

四:自定義日志打印

  1. 實現(xiàn)MessageFormattingStrategy接口方式

public class P6SpyLogger implements MessageFormattingStrategy {    
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {        return !"".equals(sql.trim()) ? "[ " + LocalDateTime.now() + " ] --- | took "
                + elapsed + "ms | " + category + " | connection " + connectionId + "\n "
                + sql + ";" : "";
    }
}

   2.在spy.properties文件中指定方式

logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗時: %(executionTime) ms | 連接信息: %(category)-%(connectionId) | 執(zhí)行語句: %(sql)

五:spy.properties參數(shù)詳細說明

# 指定應用的日志攔截模塊,默認為com.p6spy.engine.spy.P6SpyFactory 
#modulelist=com.p6spy.engine.spy.P6SpyFactory,com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 真實JDBC driver , 多個以 逗號 分割 默認為空
#driverlist=
# 是否自動刷新 默認 flase
#autoflush=false
# 配置SimpleDateFormat日期格式 默認為空
#dateformat=
# 打印堆棧跟蹤信息 默認flase
#stacktrace=false
# 如果 stacktrace=true,則可以指定具體的類名來進行過濾。
#stacktraceclass=
# 監(jiān)測屬性配置文件是否進行重新加載
#reloadproperties=false
# 屬性配置文件重新加載的時間間隔,單位:秒 默認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 的文件名 默認 spy.log
#logfile=spy.log
# 指定是否每次是增加 Log,設置為 false 則每次都會先進行清空 默認true
#append=true
# 指定日志輸出樣式  默認為com.p6spy.engine.spy.appender.SingleLineFormat , 單行輸出 不格式化語句
#logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# 也可以采用  com.p6spy.engine.spy.appender.CustomLineFormat 來自定義輸出樣式, 默認值是%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine)
# 可用的變量為:
#   %(connectionId)            connection id
#   %(currentTime)             當前時間
#   %(executionTime)           執(zhí)行耗時
#   %(category)                執(zhí)行分組
#   %(effectiveSql)            提交的SQL 換行
#   %(effectiveSqlSingleLine)  提交的SQL 不換行顯示
#   %(sql)                     執(zhí)行的真實SQL語句,已替換占位
#   %(sqlSingleLine)           執(zhí)行的真實SQL語句,已替換占位 不換行顯示
#customLogMessageFormat=%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine)
# date類型字段記錄日志時使用的日期格式 默認dd-MMM-yy
#databaseDialectDateFormat=dd-MMM-yy
# boolean類型字段記錄日志時使用的日期格式 默認boolean 可選值numeric
#databaseDialectBooleanFormat=boolean
# 是否通過jmx暴露屬性 默認true
#jmx=true
# 如果jmx設置為true 指定通過jmx暴露屬性時的前綴 默認為空
# com.p6spy(.<jmxPrefix>)?:name=<optionsClassName>
#jmxPrefix=
# 是否顯示納秒 默認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ù)源配置 
# 設置 JNDI 數(shù)據(jù)源的 NamingContextFactory。 
#jndicontextfactory=org.jnp.interfaces.NamingContextFactory
# 設置 JNDI 數(shù)據(jù)源的提供者的 URL。 
#jndicontextproviderurl=localhost:1099
# 設置 JNDI 數(shù)據(jù)源的一些定制信息,以分號分隔。 
#jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.naming:org.jnp.interfaces
# 是否開啟日志過濾 默認false, 這項配置是否生效前提是配置了 include/exclude/sqlexpression
#filter=false
# 過濾 Log 時所包含的表名列表,以逗號分隔 默認為空
#include=
# 過濾 Log 時所排除的表名列表,以逗號分隔 默認為空
#exclude=
# 過濾 Log 時的 SQL 正則表達式名稱  默認為空
#sqlexpression=
#顯示指定過濾 Log 時排隊的分類列表,取值: error, info, batch, debug, statement,
#commit, rollback, result and resultset are valid values
# (默認 info,debug,result,resultset,batch)
#excludecategories=info,debug,result,resultset,batch
# 是否過濾二進制字段
# (default is false)
#excludebinary=false
# P6Log 模塊執(zhí)行時間設置,整數(shù)值 (以毫秒為單位),只有當超過這個時間才進行記錄 Log。 默認為0
#executionThreshold=
# P6Outage 模塊是否記錄較長時間運行的語句 默認false
# outagedetection=true|false
# P6Outage 模塊執(zhí)行時間設置,整數(shù)值 (以秒為單位)),只有當超過這個時間才進行記錄 Log。 默認30s
# outagedetectioninterval=integer time (seconds)

關于Spring Boot結合P6Spy的實例分析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI