引言
最近換了號(hào)稱最快的HikariDataSource,由于沒(méi)有了SQL監(jiān)控,加之于Mybaits默認(rèn)輸出日志之拙計(jì)。遂用此物,與之相仿還有log4jdbc,比較活躍度后選擇了P6Spy。
版本
- P6Sspy: 3.7
- Gradle: 4.10
- Spring Boot: 2.0.4
步驟
具體步驟比較簡(jiǎn)單,這里說(shuō)下簡(jiǎn)單原理。P6Spy通過(guò)劫持JDBC驅(qū)動(dòng),在調(diào)用實(shí)際JDBC驅(qū)動(dòng)前攔截調(diào)用的目標(biāo)語(yǔ),達(dá)到SQL語(yǔ)句日志記錄的目的。
它包括P6Log和P6Outage兩個(gè)模塊。
- P6Log 用來(lái)攔截和記錄任務(wù)應(yīng)用程序的 JDBC 語(yǔ)句
- P6Outage 專門用來(lái)檢測(cè)和記錄超過(guò)配置條件里時(shí)間的 SQL 語(yǔ)句
應(yīng)用P6Spy只需要
- 1.替換你的
JDBC Driver為com.p6spy.engine.spy.P6SpyDriver - 2.修改
JDBC Url為jdbc:p6spy:xxxx - 3.配置
spy.properties
1.修改application.yml文件,替換jdbc driver和url
# 數(shù)據(jù)源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:p6spy:mysql://...
username: ...
password: ...
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
2.配置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
# 設(shè)置使用p6spy driver來(lái)做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實(shí)際驅(qū)動(dòng)
driverlist=com.mysql.jdbc.Driver
# 是否開(kāi)啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標(biāo)準(zhǔn) 秒
outagedetectioninterval=2
3.自定義日志打印 , 這里有兩種方式
一、實(shí)現(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 + ";" : "";
}
}
二、在 spy.properties中指定
# 自定義日志打印
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗時(shí): %(executionTime) ms | 連接信息: %(category)-%(connectionId) | 執(zhí)行語(yǔ)句: %(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)進(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類型字段記錄日志時(shí)使用的日期格式 默認(rèn)dd-MMM-yy
#databaseDialectDateFormat=dd-MMM-yy
# boolean類型字段記錄日志時(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á)式名稱 默認(rèn)為空
#sqlexpression=
#顯示指定過(guò)濾 Log 時(shí)排隊(duì)的分類列表,取值: 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)