package com.mortals.xhx.base.framework.config;

import com.mortals.framework.util.DateUtils;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;

/**
 * 自定义 p6spy sql输出格式
 *
 * @author: zxfei
 * @date: 2022/4/19 9:01
 */
@CommonsLog
public class P6spySqlFormatConfig implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {

        return StringUtils.isNotBlank(sql) ? DateUtils.getCurrStrDateTime()
                + " | 耗时 " + elapsed + " ms | SQL:" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : "";
    }
}
