<dependency>
<groupId>p6spygroupId>
<artifactId>p6spyartifactId>
<version>3.9.1version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>6.1.6.Finalversion>
dependency>
spring:
datasource:
host: jdbc:p6spy:mysql://xxxxxx:3306
url: ${spring.datasource.host}/xxxx?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
username: xxxxxx
password: xxxxxx
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
initialSize: 5
minIdle: 5
maxActive: 20
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 使用日志系统记录sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 是否开启日志过滤 默认false, 这项配置是否生效前提是配置了 include/exclude/sqlexpression
filter=true
## 过滤 Log 时所排除的表名列表,以逗号分隔。
exclude=QRTZ_SCHEDULER_STATE,QRTZ_TRIGGERS,QRTZ_LOCKS,QRTZ_FIRED_TRIGGERS,dual
## 配置记录Log例外
excludecategories=info,debug,result,commit,resultset
# 设置使用p6spy driver来做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
databaseDialectTimestampFormat = yyyy-MM-dd HH:mm:ss
# 实际驱动
driverlist=com.mysql.cj.jdbc.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 秒
outagedetectioninterval=2
# mybatisplus自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
spy.properties配置
#自定义日志格式,在类中定义
logMessageFormat=com.xxx.config.P6SpyLogger
package com.xxx.config;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;
import org.hibernate.engine.jdbc.internal.Formatter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* p6spy代理_格式化SQL输出控制台
*
* @date:2022/9/17
* @author:WángHóngKǎi
* @company:利滚利科技有限公司
*/
public class P6SpyLogger implements MessageFormattingStrategy {
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private static final Formatter formatter;
static {
formatter = new BasicFormatterImpl();
}
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
StringBuilder sb = new StringBuilder();
return !"".equals(sql.trim()) ? sb.append(this.format.format(new Date())).append(" | took ").append(elapsed).append("ms | ").append(category).append(" | connection ").append(connectionId).append(formatter.format(sql)).append(";").toString() : "";
}
}
成功效果
PS: 如果有帮助到你,请为我点个 👍👍👍 赞 👍👍👍
❤️不定期推送Java基础、设计模式、数据库知识、全栈分布式微服务架构等 😄😄😄
❤️关注就是我最大的动力,💖💖💖 感谢你这么优秀还关注我 💖💖💖一起学习,获得更多收获 😍😍😍