mirror of https://github.com/elunez/eladmin
refactor: 替换log4jdbc为p6spy,优化SQL日志输出
parent
4454b71bfd
commit
75df46b5dc
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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 {
|
||||
|
||||
// 重置颜色
|
||||
private static final String RESET = "\u001B[0m";
|
||||
// 红色
|
||||
private static final String RED = "\u001B[31m";
|
||||
// 绿色
|
||||
private static final String GREEN = "\u001B[32m";
|
||||
|
||||
/**
|
||||
* 格式化 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(
|
||||
"%s[Time: %dms]%s - %s%s%s;",
|
||||
GREEN, elapsed, RESET, RED, sql, RESET
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,10 +3,10 @@ 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: 20
|
||||
# 最小空闲连接数,保持足够的空闲连接以应对请求
|
||||
|
@ -86,9 +86,9 @@ jwt:
|
|||
# 令牌过期时间 此处单位/毫秒 ,默认4小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
|
||||
token-validity-in-seconds: 14400000
|
||||
# 在线用户key
|
||||
online-key: "online-token:"
|
||||
online-key: "online_token:"
|
||||
# 验证码
|
||||
code-key: "captcha-code:"
|
||||
code-key: "captcha_code:"
|
||||
# token 续期检查时间范围(默认30分钟,单位毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期
|
||||
detect: 1800000
|
||||
# 续期时间范围,默认1小时,单位毫秒
|
||||
|
|
|
@ -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}
|
||||
# 初始连接数,建议设置为与最小空闲连接数相同
|
||||
|
@ -90,9 +90,9 @@ jwt:
|
|||
# 令牌过期时间 此处单位/毫秒 ,默认2小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
|
||||
token-validity-in-seconds: 7200000
|
||||
# 在线用户key
|
||||
online-key: "online-token:"
|
||||
online-key: "online_token:"
|
||||
# 验证码
|
||||
code-key: "captcha-code:"
|
||||
code-key: "captcha_code:"
|
||||
# token 续期检查时间范围(默认30分钟,单位默认毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期
|
||||
detect: 1800000
|
||||
# 续期时间范围,默认 1小时,这里单位毫秒
|
||||
|
|
|
@ -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>
|
||||
</configuration>
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
# 应用的拦截模块
|
||||
modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
|
||||
|
||||
# 自定义日志打印
|
||||
logMessageFormat=me.zhengjie.config.CustomP6SpyLogger
|
||||
|
||||
# 日志输出到控制台
|
||||
appender=com.p6spy.engine.spy.appender.Slf4JLogger
|
||||
|
||||
# 日期格式
|
||||
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
|
11
pom.xml
11
pom.xml
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>me.zhengjie</groupId>
|
||||
|
@ -32,7 +30,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.19</druid.version>
|
||||
|
@ -98,9 +95,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