1. 程式人生 > >使用P6Spy監控你的Spring boot資料庫操作

使用P6Spy監控你的Spring boot資料庫操作

開發十年,就只剩下這套架構體系了! >>>   

引言

最近換了號稱最快的HikariDataSource,由於沒有了SQL監控,加之於Mybaits預設輸出日誌之拙計。遂用此物,與之相仿還有log4jdbc,比較活躍度後選擇了P6Spy

步驟

具體步驟比較簡單,這裡說下簡單原理。P6Spy通過劫持JDBC驅動,在呼叫實際JDBC驅動前攔截呼叫的目標語,達到SQL語句日誌記錄的目的。

它包括P6LogP6Outage兩個模組。

  • P6Log 用來攔截和記錄任務應用程式的 JDBC 語句
  • P6Outage 專門用來檢測和記錄超過配置條件裡時間的 SQL 語句

應用P6Spy只需要

  • 1.替換你的JDBC Drivercom.p6spy.engine.spy.P6SpyDriver
  • 2.修改JDBC Urljdbc:p6spy:xxxx
  • 3.配置spy.properties

配置spy.properties

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定義日誌列印
logMessageFormat=org.pkaq.core.log.P6SpyLogger
# 使用日誌系統記錄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
# 實際驅動 
driverlist=com.mysql.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標準 秒
outagedetectioninterval=2

自定義日誌列印 , 這裡有兩種方式

1.實現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耗時: %(executionTime) ms | 連線資訊: %(category)-%(connectionId) | 執行語句: %(sql)

附錄: spy.properties詳細說明

# 指定應用的日誌攔截模組,預設為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=

# 監測屬性配置檔案是否進行重新載入
#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)           執行耗時
#   %(category)                執行分組
#   %(effectiveSql)            提交的SQL 換行
#   %(effectiveSqlSingleLine)  提交的SQL 不換行顯示
#   %(sql)                     執行的真實SQL語句,已替換佔位
#   %(sqlSingleLine)           執行的真實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

# 實際資料來源 JNDI
#realdatasource=/RealMySqlDS
# 實際資料來源 datasource class
#realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource

# 實際資料來源所攜帶的配置引數 以 k=v 方式指定 以 分號 分割
#realdatasourceproperties=port;3306,serverName;myhost,databaseName;jbossdb,foo;bar

# jndi資料來源配置 
# 設定 JNDI 資料來源的 NamingContextFactory。 
#jndicontextfactory=org.jnp.interfaces.NamingContextFactory
# 設定 JNDI 資料來源的提供者的 URL。 
#jndicontextproviderurl=localhost:1099
# 設定 JNDI 資料來源的一些定製資訊,以分號分隔。 
#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 模組執行時間設定,整數值 (以毫秒為單位),只有當超過這個時間才進行記錄 Log。 預設為0
#executionThreshold=

# P6Outage 模組是否記錄較長時間執行的語句 預設false
# outagedetection=true|false
# P6Outage 模組執行時間設定,整數值 (以秒為單位)),只有當超過這個時間才進行記錄 Log。 預設30s
# outagedetectioninterval=integer time (seconds)