refactor: 替换log4jdbc为p6spy并优化配置
This commit is contained in:
parent
d7e4cb7e6f
commit
3de776746e
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
# 最小连接数
|
||||
|
@ -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}
|
||||
# 初始连接数
|
||||
|
@ -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
|
@ -16,30 +16,4 @@
|
||||
<root level="info">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
<!--监控sql日志输出,如需监控 Sql 打印,请设置为 INFO -->
|
||||
<logger name="jdbc.sqlonly" level="ERROR" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<logger name="jdbc.resultset" level="ERROR" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<!-- 如想看到表格数据,将OFF改为INFO -->
|
||||
<logger name="jdbc.resultsettable" level="OFF" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<logger name="jdbc.connection" level="OFF" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<logger name="jdbc.sqltiming" level="OFF" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<logger name="jdbc.audit" level="OFF" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
</configuration>
|
20
eladmin/eladmin-system/src/main/resources/spy.properties
Normal file
20
eladmin/eladmin-system/src/main/resources/spy.properties
Normal file
@ -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
|
@ -32,7 +32,6 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<log4jdbc.version>1.16</log4jdbc.version>
|
||||
<swagger.version>2.9.2</swagger.version>
|
||||
<fastjson.version>1.2.83</fastjson.version>
|
||||
<druid.version>1.2.8</druid.version>
|
||||
@ -98,9 +97,9 @@
|
||||
|
||||
<!--监控sql日志-->
|
||||
<dependency>
|
||||
<groupId>org.bgee.log4jdbc-log4j2</groupId>
|
||||
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
|
||||
<version>${log4jdbc.version}</version>
|
||||
<groupId>p6spy</groupId>
|
||||
<artifactId>p6spy</artifactId>
|
||||
<version>3.9.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Swagger UI 相关 -->
|
||||
|
Loading…
Reference in New Issue
Block a user