From f369e648e393f28b25952d61aacb165507f64810 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Wed, 4 Sep 2024 10:08:08 +0800 Subject: [PATCH] =?UTF-8?q?release:=20sql=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/luoo/user/config/P6SpyLogger.java | 47 +++++++++++++++++++ luoo_user/src/main/resources/spy.properties | 10 ++++ 2 files changed, 57 insertions(+) create mode 100644 luoo_user/src/main/java/com/luoo/user/config/P6SpyLogger.java create mode 100644 luoo_user/src/main/resources/spy.properties 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