mirror of
https://gitee.com/smallc/blade-tool.git
synced 2026-07-15 20:02:09 +08:00
🎉 4.10.1.RELEASE 新增 SqlLogUtil 支持按线程忽略 SQL 日志打印,新增 SQL 日志关键词排除逻辑
This commit is contained in:
parent
08fa78eac3
commit
1470c19128
|
|
@ -1,5 +1,5 @@
|
|||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/Release-V4.10.0-blue.svg" alt="Downloads">
|
||||
<img src="https://img.shields.io/badge/Release-V4.10.1-blue.svg" alt="Downloads">
|
||||
<img src="https://img.shields.io/badge/JDK-17+-green.svg" alt="Build Status">
|
||||
<img src="https://img.shields.io/badge/license-LGPL%20v3-blue.svg" alt="Build Status">
|
||||
<img src="https://img.shields.io/badge/Spring%20Cloud-2025-blue.svg" alt="Coverage Status">
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@
|
|||
<!-- 业务日志 -->
|
||||
<Logger name="org.springblade" level="DEBUG" />
|
||||
<Logger name="org.springblade.core.version" level="INFO"/>
|
||||
<Logger name="org.springblade.core.tenant.BladeTenantInterceptor" level="INFO"/>
|
||||
|
||||
<!-- 减少nacos日志 -->
|
||||
<logger name="com.alibaba.nacos" level="ERROR"/>
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
|
|||
import com.alibaba.druid.proxy.jdbc.StatementProxy;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.mp.props.MybatisPlusProperties;
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
|
|
@ -44,11 +43,20 @@ import java.util.List;
|
|||
* @author L.cm
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SqlLogInterceptor extends FilterEventAdapter {
|
||||
private static final SQLUtils.FormatOption FORMAT_OPTION = new SQLUtils.FormatOption(false, false);
|
||||
|
||||
private static final List<String> SQL_LOG_EXCLUDE = new ArrayList<>();
|
||||
|
||||
private final MybatisPlusProperties properties;
|
||||
|
||||
public SqlLogInterceptor(MybatisPlusProperties properties) {
|
||||
this.properties = properties;
|
||||
if (!properties.getSqlLogExclude().isEmpty()) {
|
||||
SQL_LOG_EXCLUDE.addAll(properties.getSqlLogExclude());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void statementExecuteBefore(StatementProxy statement, String sql) {
|
||||
statement.setLastExecuteStartNano();
|
||||
|
|
@ -93,6 +101,10 @@ public class SqlLogInterceptor extends FilterEventAdapter {
|
|||
public void statement_close(FilterChain chain, StatementProxy statement) throws SQLException {
|
||||
// 先调用父类关闭 statement
|
||||
super.statement_close(chain, statement);
|
||||
// 线程标记忽略 sql 日志
|
||||
if (SqlLogUtil.isIgnore()) {
|
||||
return;
|
||||
}
|
||||
// 支持动态关闭
|
||||
if (!properties.isSqlLog()) {
|
||||
return;
|
||||
|
|
@ -107,6 +119,10 @@ public class SqlLogInterceptor extends FilterEventAdapter {
|
|||
if (StringUtils.isEmpty(sql)) {
|
||||
return;
|
||||
}
|
||||
// sql 包含排除的关键字直接返回
|
||||
if (excludeSql(sql)) {
|
||||
return;
|
||||
}
|
||||
int parametersSize = statement.getParametersSize();
|
||||
List<Object> parameters = new ArrayList<>(parametersSize);
|
||||
for (int i = 0; i < parametersSize; ++i) {
|
||||
|
|
@ -138,4 +154,14 @@ public class SqlLogInterceptor extends FilterEventAdapter {
|
|||
log.info(sqlLogger, sql.trim(), StringUtil.format(statement.getLastExecuteTimeNano()));
|
||||
}
|
||||
|
||||
private static boolean excludeSql(String sql) {
|
||||
// 判断关键字
|
||||
for (String exclude : SQL_LOG_EXCLUDE) {
|
||||
if (sql.contains(exclude)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 org.springblade.core.mp.plugins;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Sql 日志工具,按线程标记忽略 sql 日志打印
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@UtilityClass
|
||||
public class SqlLogUtil {
|
||||
|
||||
/**
|
||||
* sql 日志忽略线程
|
||||
*/
|
||||
private static final ThreadLocal<Boolean> SQL_LOG_IGNORE_HOLDER = new NamedThreadLocal<>("blade-sql-log-ignore") {
|
||||
@Override
|
||||
protected Boolean initialValue() {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 是否忽略 sql 日志
|
||||
*/
|
||||
public static Boolean isIgnore() {
|
||||
return SQL_LOG_IGNORE_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略 sql 日志 执行函数
|
||||
*
|
||||
* @param supplier supplier
|
||||
* @param <R> 泛型
|
||||
* @return R 函数返回
|
||||
*/
|
||||
public static <R> R ignore(Supplier<R> supplier) {
|
||||
try {
|
||||
SQL_LOG_IGNORE_HOLDER.set(Boolean.TRUE);
|
||||
return supplier.get();
|
||||
} finally {
|
||||
SQL_LOG_IGNORE_HOLDER.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略 sql 日志 执行函数
|
||||
*
|
||||
* @param runnable Runnable
|
||||
*/
|
||||
public static void ignore(Runnable runnable) {
|
||||
try {
|
||||
SQL_LOG_IGNORE_HOLDER.set(Boolean.TRUE);
|
||||
runnable.run();
|
||||
} finally {
|
||||
SQL_LOG_IGNORE_HOLDER.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,6 +18,9 @@ package org.springblade.core.mp.props;
|
|||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MybatisPlus配置类
|
||||
*
|
||||
|
|
@ -42,4 +45,9 @@ public class MybatisPlusProperties {
|
|||
*/
|
||||
private boolean sqlLog = true;
|
||||
|
||||
/**
|
||||
* sql日志忽略打印关键字
|
||||
*/
|
||||
private List<String> sqlLogExclude = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user