添加依赖:
<!-- sql 打印 -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.1</version>
</dependency>
<!-- commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
config:
/**
* 自定义 p6spy sql输出格式
*
* @author David
*/
public class P6spySqlFormatConfig implements MessageFormattingStrategy {
/**
* 过滤掉定时任务的 SQL
*/
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
return StringUtils.isNotBlank(sql) ? DateUtil.formatFullTime(LocalDateTime.now(), DateUtil.FULL_TIME_SPLIT_PATTERN)
+ " | 耗时 " + elapsed + " ms | SQL 语句:" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : "";
}
}
spy.properties:
# p6spy\u914D\u7F6E\uFF0C\u6587\u6863 https://p6spy.readthedocs.io/en/latest/configandusage.html
# \u4F7F\u7528\u65E5\u5FD7\u7CFB\u7EDF\u8BB0\u5F55 sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# \u81EA\u5B9A\u4E49\u65E5\u5FD7\u6253\u5370
logMessageFormat=com.common.config.P6spySqlFormatConfig
# \u662F\u5426\u5F00\u542F\u6162 SQL\u8BB0\u5F55
outagedetection=true
# \u6162 SQL\u8BB0\u5F55\u6807\u51C6 2 \u79D2
outagedetectioninterval=2
# \u5F00\u542F\u8FC7\u6EE4
filter=true
# \u5305\u542B QRTZ\u7684\u4E0D\u6253\u5370
exclude=QRTZ
datesources:url和drivername和以往不同
datasource:
url: jdbc:p6spy:mysql://localhost:3306/test
driver-class-name: com.p6spy.engine.spy.P6SpyDriver