diff --git a/luoo_user/src/main/java/com/luoo/user/config/P6SpyLogger.java b/luoo_user/src/main/java/com/luoo/user/config/P6SpyLogger.java new file mode 100644 index 0000000..585b530 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/config/P6SpyLogger.java @@ -0,0 +1,47 @@ +package com.luoo.user.config; + +/** + * @program: luoo_parent + * @description: + * @author: yawei.huang + * @create: 2024-09-04 10:02 + **/ +import com.p6spy.engine.spy.appender.MessageFormattingStrategy; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class P6SpyLogger implements MessageFormattingStrategy { + + private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); + + /** + * 自定义sql日志打印 + * + * @param connectionId 连接标识 + * @param now 执行时间 + * @param elapsed 执行秒数ms + * @param category statement + * @param prepared 准备sql语句 + * @param sql sql语句 + * @param s4 数据库url连接 + * @return {@link String} + */ + @Override + public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String s4) { + System.out.println(); + if (!"".equals(sql.trim())) { + String sqlBegin = "============== SQL LOGGER BEGIN =============="; + + String sqlExecuteTime = "SQL 执行时间 :" + this.format.format(new Date()) + "\n"; + String elapsedStr = "SQL 执行毫秒 :" + elapsed + "ms" + "\n"; + String sqlPrint = "SQL 执行语句 :" + sql; + + //String sqlPrint = !"".equals(sql.trim()) ? this.format.format(new Date()) + " | took " + elapsed + "ms | " + category + " | connection " + connectionId + "\n " + sql + ";" : ""; + String sqlEnd = "============== SQL LOGGER END =============="; + + return sqlBegin + "\r\n" + sqlExecuteTime + elapsedStr + sqlPrint + "\r\n" + sqlEnd; + } + return ""; + } +} diff --git a/luoo_user/src/main/resources/spy.properties b/luoo_user/src/main/resources/spy.properties new file mode 100644 index 0000000..3203652 --- /dev/null +++ b/luoo_user/src/main/resources/spy.properties @@ -0,0 +1,10 @@ +module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory +appender=com.p6spy.engine.spy.appender.StdoutLogger +excludecategories=info,debug,result,batc,resultset +deregisterdrivers=true +dateformat=yyyy-MM-dd HH:mm:ss +outagedetection=true +outagedetectioninterval=2 + +#???SQL????? +logMessageFormat=com.luoo.user.config.P6SpyLogger \ No newline at end of file