diff --git a/eladmin/eladmin-common/src/main/java/me/zhengjie/config/CustomP6SpyLogger.java b/eladmin/eladmin-common/src/main/java/me/zhengjie/config/CustomP6SpyLogger.java new file mode 100644 index 0000000..2f506f3 --- /dev/null +++ b/eladmin/eladmin-common/src/main/java/me/zhengjie/config/CustomP6SpyLogger.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.zhengjie.config; + +import cn.hutool.core.util.StrUtil; +import com.p6spy.engine.spy.appender.MessageFormattingStrategy; +import lombok.extern.slf4j.Slf4j; + +/** + * @author Zheng Jie + * @description 自定义 p6spy sql输出格式 + * @date 2024-12-26 + **/ +@Slf4j +public class CustomP6SpyLogger implements MessageFormattingStrategy { + + /** + * 格式化 sql + * @param connectionId 连接id + * @param now 当前时间 + * @param elapsed 执行时长 + * @param category sql分类 + * @param prepared 预编译sql + * @param sql 执行sql + * @param url 数据库连接url + * @return 格式化后的sql + */ + @Override + public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { + // 去掉换行和多余空格 + if(StrUtil.isNotBlank(sql)){ + sql = sql.replaceAll("\\s+", " ").trim(); + } + return String.format("sql- %s [Time: %dms] - %s;", now, elapsed, sql); + } +} diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/AppRun.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/AppRun.java index 0888ff4..442448c 100644 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/AppRun.java +++ b/eladmin/eladmin-system/src/main/java/me/zhengjie/AppRun.java @@ -27,8 +27,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.bind.annotation.RestController; /** - * 开启审计功能 -> @EnableJpaAuditing - * * @author Zheng Jie * @date 2018/11/15 9:20:19 */ diff --git a/eladmin/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin/eladmin-system/src/main/resources/config/application-dev.yml index 18c0bf3..31320d6 100644 --- a/eladmin/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin/eladmin-system/src/main/resources/config/application-dev.yml @@ -1,17 +1,12 @@ -# Sql日志 -mybatis-plus: - configuration: - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl - #配置数据源 spring: datasource: druid: db-type: com.alibaba.druid.pool.DruidDataSource - driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false - username: ${DB_USER:root} - password: ${DB_PWD:123456} + driverClassName: com.p6spy.engine.spy.P6SpyDriver + url: jdbc:p6spy:mysql://localhost:3306/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false + username: root + password: 123456 # 初始连接数 initial-size: 5 # 最小连接数 diff --git a/eladmin/eladmin-system/src/main/resources/config/application-prod.yml b/eladmin/eladmin-system/src/main/resources/config/application-prod.yml index 39d6a11..aae8dd4 100644 --- a/eladmin/eladmin-system/src/main/resources/config/application-prod.yml +++ b/eladmin/eladmin-system/src/main/resources/config/application-prod.yml @@ -3,8 +3,8 @@ spring: datasource: druid: db-type: com.alibaba.druid.pool.DruidDataSource - driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false username: ${DB_USER:root} password: ${DB_PWD:123456} # 初始连接数 diff --git a/eladmin/eladmin-system/src/main/resources/log4jdbc.log4j2.properties b/eladmin/eladmin-system/src/main/resources/log4jdbc.log4j2.properties deleted file mode 100644 index 302525f..0000000 --- a/eladmin/eladmin-system/src/main/resources/log4jdbc.log4j2.properties +++ /dev/null @@ -1,4 +0,0 @@ -# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger -log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator -log4jdbc.auto.load.popular.drivers=false -log4jdbc.drivers=com.mysql.cj.jdbc.Driver \ No newline at end of file diff --git a/eladmin/eladmin-system/src/main/resources/logback.xml b/eladmin/eladmin-system/src/main/resources/logback.xml index 3f8f9d8..70c41d8 100644 --- a/eladmin/eladmin-system/src/main/resources/logback.xml +++ b/eladmin/eladmin-system/src/main/resources/logback.xml @@ -16,30 +16,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/eladmin/eladmin-system/src/main/resources/spy.properties b/eladmin/eladmin-system/src/main/resources/spy.properties new file mode 100644 index 0000000..ff9fafb --- /dev/null +++ b/eladmin/eladmin-system/src/main/resources/spy.properties @@ -0,0 +1,20 @@ +# 应用的拦截模块 +modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory +# 自定义日志打印 +logMessageFormat=me.zhengjie.config.CustomP6SpyLogger +# 日志输出到控制台 +appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger +# 日期格式 +dateformat=yyyy-MM-dd HH:mm:ss +# 实际驱动 可多个 +driverlist=com.mysql.cj.jdbc.Driver +# 是否开启慢SQL记录 +outagedetection=true +# 慢SQL记录标准 2 秒 +outagedetectioninterval=2 +# 是否过滤 Log +filter=true +# 过滤 Log 时所排除的 sql 关键字,以逗号分隔 +exclude=select 1 +# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset. +excludecategories=info,debug,result,commit,resultset \ No newline at end of file diff --git a/eladmin/pom.xml b/eladmin/pom.xml index 403acdf..db42124 100644 --- a/eladmin/pom.xml +++ b/eladmin/pom.xml @@ -32,7 +32,6 @@ UTF-8 UTF-8 1.8 - 1.16 2.9.2 1.2.83 1.2.8 @@ -98,9 +97,9 @@ - org.bgee.log4jdbc-log4j2 - log4jdbc-log4j2-jdbc4.1 - ${log4jdbc.version} + p6spy + p6spy + 3.9.1