playframework 1.x 使用 P6spy监控打印SQL语句

1、下载最新的3.0版本zip,解压出p6spy.jar 和 spy.properties两个文件
2、将p6spy.jar 放入应用程序的lib目录,将spy.properties放入conf目录
3、数据库如果为mysql,修改spy.properties , realdriver=com.mysql.jdbc.Driver
4、修改application.conf文件中db.default.driver=com.p6spy.engine.spy.P6SpyDriver
5、修改db.default.url=jdbc:p6spy:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=UTF-8

6、(最新版未出现)如果spy.log里出现
  你的程序的数据库驱动名称 is a real driver in spy.properties, but it has been loaded before p6spy . p6spy will not wrap these connections. Either prevent the driver from loading, or try setting ‘deregisterdrivers’ to true in spy.properties
  请把spy.properties文件里的deregisterdrivers=false改为deregisterdrivers=true,重新运行即可。
  7、自定义输出可以新建一个类继承MessageFormattingStrategy
  

1
2
3
4
5
6
7
8
9
10
11
12
13
  package utils;
  
  import com.p6spy.engine.common.P6Util;
  import com.p6spy.engine.spy.appender.*;
 
  public class CustomSqlFormat implements MessageFormattingStrategy {
  @Override
  public String formatMessage(final int connectionId, final String now, final long elapsed, final String category,
  final String prepared, final String sql) {
  return now + "|" + elapsed + "|" + category + "|connection " + connectionId //+ "|" + P6Util.singleLine(prepared)
  + "|" + P6Util.singleLine(sql);
  }
  }

  8、修改spy.properties文件中logMessageFormat=utils.CustomSqlFormat
  9、重启play项目,查看spy.log
  下载地址
  http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22p6spy%22%20AND%20a%3A%22p6spy%22
  参考
  http://p6spy.readthedocs.io/en/latest/configandusage.html
  http://www.mkyong.com/hibernate/how-to-display-hibernate-sql-parameter-values-solution/
  http://panyongzheng.iteye.com/blog/2205748
  http://blog.csdn.net/zzg1229059735/article/details/73477748
  http://www.it610.com/article/2563704.htm