用p6spy和sqlprofiler来进行jdbc sql日志记录和分析
1.相关工具下载:
p6spy: http://heanet.dl.sourceforge.net/sourceforge/p6spy/p6spy-install.zip
http://www.p6spy.com/download.html
辅助日志分析工具:
http://heanet.dl.sourceforge.net/sourceforge/sqlprofiler/sqlprofiler-0.3-bin.zip
http://www.jahia.org/jahia/page377.html
2.解压p6spy-install.zip,把p6spy.jar和spy.properties放到classpath中。
修改spy.properties
realdriver=org.gjt.mm.mysql.Driver# 数据库驱动程序的名字
appender=com.p6spy.engine.logging.appender.StdoutLogger
3.在设置数据源或者配置数据库连接的地方,
修改driver为:com.p6spy.engine.spy.P6SpyDriver
例如:
<sql:setDataSource driver="com.p6spy.engine.spy.P6SpyDriver" user="${db_user}" password="${db_password}" url="${db_url}" var="mydb" scope="application"/>
运行程序,然后就可以在控制台看到所有的sql语句了。
4.解压sqlprofiler-0.3-bin.zip,将
sqlprofiler的spy.properties下面内容复制到先前的p6spy中的spy.properties替换相应地配置:
--------------------------
appender=com.p6spy.engine.logging.appender.Log4jLogger
#appender=com.p6spy.engine.logging.appender.StdoutLogger
#appender=com.p6spy.engine.logging.appender.FileLogger
# append to the p6spy log file. if this is set to false the
# log file is truncated every time. (file logger only)
append=true
#The following are for log4j logging only
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n
log4j.appender.SQLPROFILER_CLIENT=org.apache.log4j.net.SocketAppender
log4j.appender.SQLPROFILER_CLIENT.RemoteHost=localhost
log4j.appender.SQLPROFILER_CLIENT.Port=4445
log4j.appender.SQLPROFILER_CLIENT.LocationInfo=true
#log4j.logger.p6spy=INFO,STDOUT
log4j.logger.p6spy=DEBUG, SQLPROFILER_CLIENT
这段配置的意思是使用log4j来记录日志。
因此需要用到log4j.jar
先运行sqlprofiler.jar,然后再运行自己的应用,
这样就可以建立socket的logger了。
然后就可以在sqlprofiler的界面中看到sql语句。
参考:
http://www.javaworld.com.tw/jute/post/view?bid=21&id=93745&sty=1&tpg=1&age=0