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