diff --git a/README.md b/README.md index 6d51de8..77f9edb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- Downloads + Downloads Build Status Build Status Coverage Status diff --git a/blade-core-boot/src/main/resources/log/logback_dev.xml b/blade-core-boot/src/main/resources/log/logback_dev.xml index 40c1cb3..a1b35e0 100644 --- a/blade-core-boot/src/main/resources/log/logback_dev.xml +++ b/blade-core-boot/src/main/resources/log/logback_dev.xml @@ -76,6 +76,7 @@ + diff --git a/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java index 342cbd7..bc4bd3c 100644 --- a/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java +++ b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java @@ -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 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 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; + } + } diff --git a/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogUtil.java b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogUtil.java new file mode 100644 index 0000000..b87f58a --- /dev/null +++ b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogUtil.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com). + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 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 泛型 + * @return R 函数返回 + */ + public static R ignore(Supplier 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(); + } + } + +} diff --git a/blade-starter-mybatis/src/main/java/org/springblade/core/mp/props/MybatisPlusProperties.java b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/props/MybatisPlusProperties.java index 10cd47c..01df6cd 100644 --- a/blade-starter-mybatis/src/main/java/org/springblade/core/mp/props/MybatisPlusProperties.java +++ b/blade-starter-mybatis/src/main/java/org/springblade/core/mp/props/MybatisPlusProperties.java @@ -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 sqlLogExclude = new ArrayList<>(); + } diff --git a/pom.xml b/pom.xml index cf36a97..30a8a55 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,7 @@ - 4.10.0 + 4.10.1 org.springblade.blade.tool 17