From 8fa4829c0358390180b5eeba6898149ff6f43689 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Thu, 1 Aug 2024 10:57:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20p6spy=20=EC=84=A4=EC=A0=95=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ens/core/config/logging/P6SpyConfig.java | 17 +++++++++++++++++ .../core/config/logging/P6SpyEventListener.java | 15 +++++++++++++++ ...atConfiguration.java => P6SpyFormatter.java} | 14 +++++++++----- .../core/config/logging/P6spyLoggingConfig.java | 9 +++++---- src/main/resources/application.yml | 1 + 5 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/main/java/cokr/xit/ens/core/config/logging/P6SpyConfig.java create mode 100644 src/main/java/cokr/xit/ens/core/config/logging/P6SpyEventListener.java rename src/main/java/cokr/xit/ens/core/config/logging/{P6spySqlFormatConfiguration.java => P6SpyFormatter.java} (81%) diff --git a/src/main/java/cokr/xit/ens/core/config/logging/P6SpyConfig.java b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyConfig.java new file mode 100644 index 0000000..4cfe052 --- /dev/null +++ b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyConfig.java @@ -0,0 +1,17 @@ +package cokr.xit.ens.core.config.logging; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class P6SpyConfig { + + @Bean + public P6SpyEventListener p6SpyCustomEventListener() { + return new P6SpyEventListener(); + } + @Bean + public P6SpyFormatter p6SpyCustomFormatter() { + return new P6SpyFormatter(); + } +} diff --git a/src/main/java/cokr/xit/ens/core/config/logging/P6SpyEventListener.java b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyEventListener.java new file mode 100644 index 0000000..b96b680 --- /dev/null +++ b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyEventListener.java @@ -0,0 +1,15 @@ +package cokr.xit.ens.core.config.logging; + +import java.sql.SQLException; + +import com.p6spy.engine.common.ConnectionInformation; +import com.p6spy.engine.event.JdbcEventListener; +import com.p6spy.engine.spy.P6SpyOptions; + +public class P6SpyEventListener extends JdbcEventListener { + + @Override + public void onAfterGetConnection(ConnectionInformation connectionInformation, SQLException e) { + P6SpyOptions.getActiveInstance().setLogMessageFormat(P6SpyFormatter.class.getName()); + } +} diff --git a/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyFormatter.java similarity index 81% rename from src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java rename to src/main/java/cokr/xit/ens/core/config/logging/P6SpyFormatter.java index 79feb06..8ad67c2 100644 --- a/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java +++ b/src/main/java/cokr/xit/ens/core/config/logging/P6SpyFormatter.java @@ -10,19 +10,22 @@ import com.p6spy.engine.spy.appender.MessageFormattingStrategy; import lombok.extern.slf4j.Slf4j; @Slf4j -public class P6spySqlFormatConfiguration implements MessageFormattingStrategy { +public class P6SpyFormatter implements MessageFormattingStrategy { private static final String NEW_LINE = "\n"; private static final String TAP = "\t"; private static final String CREATE = "create"; private static final String ALTER = "alter"; private static final String DROP = "drop"; private static final String COMMENT = "comment"; + private static final String COMMIT = "commit"; @Override public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { + if(category.equals(COMMIT)) return "Connection ID: " + connectionId + " " + COMMIT; if (sql.trim().isEmpty()) { return formatByCommand(category); - } return formatBySql(sql, category) + getAdditionalMessages(elapsed); + } + return formatBySql(sql, category) + getAdditionalMessages(connectionId, elapsed); } private static String formatByCommand(String category) { return NEW_LINE @@ -43,17 +46,18 @@ public class P6spySqlFormatConfiguration implements MessageFormattingStrategy { + FormatStyle.DDL .getFormatter() .format(sql); - } return NEW_LINE + } + return NEW_LINE + "Execute DML : " + NEW_LINE + FormatStyle.BASIC .getFormatter() .format(sql); } - private String getAdditionalMessages(long elapsed) { + private String getAdditionalMessages(int connectionId, long elapsed) { return NEW_LINE + NEW_LINE - + String.format("Execution Time: %s ms", elapsed) + + String.format("Connection ID: %s Execution Time: %s ms", connectionId, elapsed) + NEW_LINE + "----------------------------------------------------------------------------------------------------"; } diff --git a/src/main/java/cokr/xit/ens/core/config/logging/P6spyLoggingConfig.java b/src/main/java/cokr/xit/ens/core/config/logging/P6spyLoggingConfig.java index 605ef6f..cb3cc20 100644 --- a/src/main/java/cokr/xit/ens/core/config/logging/P6spyLoggingConfig.java +++ b/src/main/java/cokr/xit/ens/core/config/logging/P6spyLoggingConfig.java @@ -1,9 +1,10 @@ package cokr.xit.ens.core.config.logging; -import com.p6spy.engine.spy.P6SpyOptions; +import javax.annotation.PostConstruct; + import org.springframework.context.annotation.Configuration; -import javax.annotation.PostConstruct; +import com.p6spy.engine.spy.P6SpyOptions; @Configuration public class P6spyLoggingConfig { @@ -14,6 +15,6 @@ public class P6spyLoggingConfig { */ @PostConstruct public void setLogMessageFormat() { - P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spySqlFormatConfiguration.class.getName()); + P6SpyOptions.getActiveInstance().setLogMessageFormat(P6SpyFormatter.class.getName()); } -} \ No newline at end of file +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2e0523a..f616185 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -45,6 +45,7 @@ spring: continue-on-error: false datasource: hikari: + read-only: true auto-commit: false connection-test-query: SELECT 1 minimum-idle: 10