P6Spy簡介java
P6Spy是一個能夠用來在應用程序中攔截和修改數據操做語句的開源框架。經過P6Spy咱們能夠對SQL語句進行攔截,至關於一個SQL語句的記錄器,這樣咱們能夠用它來做相關的分析,好比性能分析。mysql
P6SPY提供了以下幾個功能:正則表達式
\1. 記錄SQL語句的執行時間戳。spring
\2. 記錄SQL語句類型。sql
\3. 記錄SQL填入參數的和沒有填入參數的SQL語句。express
\4. 根據配置的時間控制SQL語句的執行時間,對超出時間的SQL語句輸出到日誌文件中。mybatis
<!--more-->app
配置步驟:框架
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency>
01.將你的JDBC驅動替換爲ide
# 修改 JDBC 驅動爲以下 P6SpyDriver 配置 # 以前爲: # spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
02.修改spring.datasource.url
# 注意是 jdbc:p6spy:mysql spring.datasource.url=jdbc:p6spy:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
03.添加配置文件 spy.properties
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory # 自定義日誌打印,改爲你自定義配置類的全類名 logMessageFormat=com.kangqing.platform.aop.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
04.自定義日誌打印
(我喜歡這個功能)能夠把 mybatis 佔位符填進去,輸入完整的sql日誌
/** * 自定義日誌 * 默認com.p6spy.engine.spy.appender.SingleLineFormat * @author kangqing * @date 2020/9/7 10:58 */ public class P6SpyLogger implements MessageFormattingStrategy { /** * 日誌格式 * @param connectionId 鏈接id * @param now 當前時間 * @param elapsed 耗時多久 * @param category 類別 * @param prepared mybatis帶佔位符的sql * @param sql 佔位符換成參數的sql * @param url sql鏈接的 url * @return 自定義格式日誌 */ @Override public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { return !"".equals(sql.trim()) ? "P6SpyLogger " + LocalDateTime.now() + " | elapsed " + elapsed + "ms | category " + category + " | connection " + connectionId + " | url " + url + " | sql \n" + 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)
參考:
https://my.oschina.net/hutaishi/blog/3020251
往期推薦