diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java new file mode 100644 index 00000000..9ff36909 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 com.mybatisflex.core.audit; + +import com.mybatisflex.core.util.DateUtil; + +import java.time.LocalDateTime; +import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; + +public class ConsoleMessageCollector implements MessageCollector { + + private SqlDebugPrinter printer = (sql, takedTimeMillis) -> { + if (takedTimeMillis != null) { + System.out.println("Flex exec sql taked " + takedTimeMillis + " ms >>> " + sql); + } else { + System.out.println("Flex exec sql >>> " + sql); + } + }; + + public ConsoleMessageCollector() { + } + + public ConsoleMessageCollector(SqlDebugPrinter printer) { + this.printer = printer; + } + + @Override + public void collect(AuditMessage message) { + String sql = getFullSql(message.getQuery(), message.getQueryParams()); + printer.print(sql, message.getElapsedTime()); + } + + private String getFullSql(String sql, List