feat: p6spy 설정 추가

main
Jonguk. Lim 4 months ago
parent 1ce0ee719a
commit 8fa4829c03

@ -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();
}
}

@ -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());
}
}

@ -10,19 +10,22 @@ import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class P6spySqlFormatConfiguration implements MessageFormattingStrategy { public class P6SpyFormatter implements MessageFormattingStrategy {
private static final String NEW_LINE = "\n"; private static final String NEW_LINE = "\n";
private static final String TAP = "\t"; private static final String TAP = "\t";
private static final String CREATE = "create"; private static final String CREATE = "create";
private static final String ALTER = "alter"; private static final String ALTER = "alter";
private static final String DROP = "drop"; private static final String DROP = "drop";
private static final String COMMENT = "comment"; private static final String COMMENT = "comment";
private static final String COMMIT = "commit";
@Override @Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { 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()) { if (sql.trim().isEmpty()) {
return formatByCommand(category); return formatByCommand(category);
} return formatBySql(sql, category) + getAdditionalMessages(elapsed); }
return formatBySql(sql, category) + getAdditionalMessages(connectionId, elapsed);
} }
private static String formatByCommand(String category) { private static String formatByCommand(String category) {
return NEW_LINE return NEW_LINE
@ -43,17 +46,18 @@ public class P6spySqlFormatConfiguration implements MessageFormattingStrategy {
+ FormatStyle.DDL + FormatStyle.DDL
.getFormatter() .getFormatter()
.format(sql); .format(sql);
} return NEW_LINE }
return NEW_LINE
+ "Execute DML : " + "Execute DML : "
+ NEW_LINE + NEW_LINE
+ FormatStyle.BASIC + FormatStyle.BASIC
.getFormatter() .getFormatter()
.format(sql); .format(sql);
} }
private String getAdditionalMessages(long elapsed) { private String getAdditionalMessages(int connectionId, long elapsed) {
return NEW_LINE return NEW_LINE
+ NEW_LINE + NEW_LINE
+ String.format("Execution Time: %s ms", elapsed) + String.format("Connection ID: %s Execution Time: %s ms", connectionId, elapsed)
+ NEW_LINE + NEW_LINE
+ "----------------------------------------------------------------------------------------------------"; + "----------------------------------------------------------------------------------------------------";
} }

@ -1,9 +1,10 @@
package cokr.xit.ens.core.config.logging; package cokr.xit.ens.core.config.logging;
import com.p6spy.engine.spy.P6SpyOptions; import javax.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct; import com.p6spy.engine.spy.P6SpyOptions;
@Configuration @Configuration
public class P6spyLoggingConfig { public class P6spyLoggingConfig {
@ -14,6 +15,6 @@ public class P6spyLoggingConfig {
*/ */
@PostConstruct @PostConstruct
public void setLogMessageFormat() { public void setLogMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spySqlFormatConfiguration.class.getName()); P6SpyOptions.getActiveInstance().setLogMessageFormat(P6SpyFormatter.class.getName());
} }
} }

@ -45,6 +45,7 @@ spring:
continue-on-error: false continue-on-error: false
datasource: datasource:
hikari: hikari:
read-only: true
auto-commit: false auto-commit: false
connection-test-query: SELECT 1 connection-test-query: SELECT 1
minimum-idle: 10 minimum-idle: 10

Loading…
Cancel
Save