极简设计

p6spy

p6spy

https://github.com/ddatsh/p6spy fork后diy一下

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

p6spy 2.1.4实测connection.driver_class用后者无效,spy.properties怎么折腾也不行


<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--<property name="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>-->

直接

<property name="connection.url">jdbc:p6spy:mysql://localhost:3306/test?characterEncoding=UTF-8</property>

spy.properties

module.log=com.p6spy.engine.logging.P6LogFactory
realdriver=com.mysql.jdbc.Driver
#excludecategories=info,debug,result,batch,resultset
excludecategories=info,debug,result,resultset
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
appender=com.ddatsh.hibernate.spy.appender.DDStdoutLogger
#logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
#logMessageFormat=com.p6spy.engine.spy.appender.MultiLineFormat
logMessageFormat=com.ddatsh.hibernate.spy.DDSpy
jmx=false

自带两个输出格式都不好看,啰嗦 SingleLineFormat

p6spy: |26|statement|connection 0|select student0_.ID as ID1_ from students student0_  limit ?, ?|select student0_.ID as ID1_  from students student0_ limit 1,1
p6spy: |0|commit|connection 0||

MultiLineFormat

p6spy: # | took 26ms | statement | connection 0|select student0_.ID as ID1_ from students student0_  limit ?, ?
select student0_.ID as ID1_  from students student0_ limit 1,1p6spy: 
# | took 0ms | commit | connection 0|

DIY,调druid格式化一下

package com.p6spy.engine.spy.appender;

import com.alibaba.druid.sql.SQLUtils;

public class DruidFormat implements MessageFormattingStrategy {

  public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {

    if (category.equals("commit") || category.equals("rollback")) {

      return category + ";";

    } else {

      if (sql.length() > 0) {

        return SQLUtils.formatMySql(sql) + ";";

      } else {
        return SQLUtils.formatMySql(prepared) + ";";
      }
    }
  }
}


com.p6spy.engine.logging.P6LogPreparedStatementExecuteDelegate  

finally {
    if(!method.getName().equals("executeBatch")){
        P6LogQuery.logElapsed(preparedStatementInformation.getConnectionId(), startTime, Category.STATEMENT, preparedStatementInformation);
    }else{
        //P6LogQuery.logElapsed(preparedStatementInformation.getConnectionId(), startTime, Category.STATEMENT, preparedStatementInformation);
}
   

p6spy: SELECT student0_.ID AS ID1_
FROM students student0_
LIMIT 1,1;
p6spy: commit;

in java with : p6spy, java