软件简介
P6Spy是一个可以用来在应用程序中拦截和修改数据操作语句的开源框架。 通过P6Spy我们可以对SQL语句进行拦截,相当于一个SQL语句的记录器,这样我们可以用它来作相关的分析,比如性能分析。
P6SPY提供了如下几个功能:
记录SQL语句的执行时间戳。
记录SQL语句类型
记录SQL填入参数的和没有填入参数的SQL语句
根据配置的时间控制SQL语句的执行时间,对超出时间的SQL语句输出到日志文件中
展示效果
可以看到打印的sql,执行时间,和打印sql所携带的参数
如何配置
1.在Pom文件中添加maven依赖
<!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
2.新增文件spy.properties
#日志格式
appender=com.p6spy.engine.spy.appender.Slf4JLogger
#输出格式
logMessageFormat=com.p6spy.engine.spy.appender.MultiLineFormat
#sql执行时间和格式
customLogMessageFormat=executionTime:%(executionTime) | sql:%(sql)
官网有相应的配置文件和参数说明,以上只是常用的几个配置属性
Configuration and Usage — p6spy 3.9.2-SNAPSHOT documentation
3.修改mysql连接驱动
数据库的驱动替换为:com.p6spy.engine.spy.P6SpyDriver
url的jdbc后面加: p6spy:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://localhost:3306/message?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true
具体配置如下:
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://localhost:3306/message?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true
这样就完全配置好了