mybatis 自定义输出日志格式

/**
 * sql 格式化
 * 参考 https://github.com/starxg/mybatis-log-plugin-free
 */


import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.logging.Log;

import java.util.*;
/**
 * @Description: sql 格式化
 * @Author: DingQiMing
 * @Date: 2023-07-17
 * @Version: V1.0
 */
public class StdOutImpl implements Log {
    private static final String PREPARING_KEY = "==>  Preparing: ";
    private static final String PARAMETERS_KEY = "==> Parameters: ";
    ThreadLocal threadLocal = new ThreadLocal<>();
    public StdOutImpl(String clazz) {
        // Do Nothing
    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public void error(String s, Throwable e) {
        System.err.println(s);
        e.printStackTrace(System.err);
    }

    @Override
    public void error(String s) {
        System.err.println(s);
    }

    @Override
    public void debug(String s) {
        System.out.println(s);
        if (s.startsWith(PREPARING_KEY)) {
            threadLocal.set(s.replaceAll(PREPARING_KEY, ""));
        }else if (s.startsWith(PARAMETERS_KEY)) {
            String sql = threadLocal.get();
            String params = s.replaceAll(PARAMETERS_KEY, "");
            String log = this.parseSql(sql,parseParams(params)).toString();
            System.err.println("==>  Log: " + log);
        }
    }

    @Override
    public void trace(String s) {
        System.out.println(s);
    }

    @Override
    public void warn(String s) {
        System.out.println(s);
    }

    private static final char MARK = '?';

    private static final Set NEED_BRACKETS;

    static {
        Set types = new HashSet<>(8);
        types.add("String");
        types.add("Date");
        types.add("Time");
        types.add("LocalDate");
        types.add("LocalTime");
        types.add("LocalDateTime");
        types.add("BigDecimal");
        types.add("Timestamp");
        NEED_BRACKETS = Collections.unmodifiableSet(types);
    }

    static StringBuilder parseSql(String sql, Queue> params) {
        final StringBuilder sb = new StringBuilder(sql);
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) != MARK) {
                continue;
            }
            final Map.Entry entry = params.poll();
            if (Objects.isNull(entry)) {
                continue;
            }
            sb.deleteCharAt(i);
            if (NEED_BRACKETS.contains(entry.getValue())) {
                sb.insert(i, String.format("'%s'", entry.getKey()));
            } else {
                sb.insert(i, entry.getKey());
            }
        }
        return sb;
    }

    static Queue> parseParams(String line) {
        line = StringUtils.removeEnd(line, "\n");
        final String[] strings = StringUtils.splitByWholeSeparator(line, ", ");
        final Queue> queue = new ArrayDeque<>(strings.length);
        for (String s : strings) {
            String value = StringUtils.substringBeforeLast(s, "(");
            String type = StringUtils.substringBetween(s, "(", ")");
            if (StringUtils.isEmpty(type)) {
                queue.offer(new AbstractMap.SimpleEntry<>(value, null));
            } else {
                queue.offer(new AbstractMap.SimpleEntry<>(value, type));
            }
        }
        return queue;
    }
}

你可能感兴趣的:(Java,mybatis,mybatis)