001/** 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.audit; 017 018import com.mybatisflex.core.util.DateUtil; 019 020import java.time.LocalDateTime; 021import java.util.Date; 022import java.util.List; 023import java.util.regex.Matcher; 024 025public class ConsoleMessageCollector implements MessageCollector { 026 027 private SqlDebugPrinter printer = (sql, takedTimeMillis) -> { 028 if (takedTimeMillis != null) { 029 System.out.println("Flex exec sql taked " + takedTimeMillis + " ms >>> " + sql); 030 } else { 031 System.out.println("Flex exec sql >>> " + sql); 032 } 033 }; 034 035 public ConsoleMessageCollector() { 036 } 037 038 public ConsoleMessageCollector(SqlDebugPrinter printer) { 039 this.printer = printer; 040 } 041 042 @Override 043 public void collect(AuditMessage message) { 044 printer.print(message.getFullSql(), message.getElapsedTime()); 045 } 046 047 public interface SqlDebugPrinter { 048 void print(String sql, Long takedTimeMillis); 049 } 050}