refactor: p6spy 로그 format 재정의

main
Jonguk. Lim 4 months ago
parent d8af5b58e7
commit 4cef3d5902

@ -1,72 +1,72 @@
package cokr.xit.ens.core.config.logging; package cokr.xit.ens.core.config.logging;
import java.util.Locale;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import com.p6spy.engine.logging.Category; import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy; import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale; import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
import java.util.Stack;
@Slf4j @Slf4j
public class P6spySqlFormatConfiguration implements MessageFormattingStrategy { public class P6spySqlFormatConfiguration implements MessageFormattingStrategy {
@Override private static final String NEW_LINE = "\n";
public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql, final String url) { private static final String TAP = "\t";
Stack<String> callStack = new Stack<>(); private static final String CREATE = "create";
StackTraceElement[] stackTrace = new Throwable().getStackTrace(); private static final String ALTER = "alter";
private static final String DROP = "drop";
for (StackTraceElement stackTraceElement : stackTrace) { private static final String COMMENT = "comment";
String trace = stackTraceElement.toString();
if (trace.startsWith("io.p6spy") && !trace.contains("P6spyPrettySqlFormatter")) {
callStack.push(trace);
}
}
StringBuilder callStackBuilder = new StringBuilder(); @Override
int order = 1; public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
while(callStack.size() != 0) { if (sql.trim().isEmpty()) {
callStackBuilder.append("\n\t\t").append(order++).append(". ").append(callStack.pop()); return formatByCommand(category);
} } return formatBySql(sql, category) + getAdditionalMessages(elapsed);
String message = new StringBuilder().append("\n\n\tConnection ID: ").append(connectionId)
.append("\n\tExecution Time: ").append(elapsed).append(" ms\n")
.append("\n\tCall Stack (number 1 is entry point): ").append(callStackBuilder).append("\n")
.toString();
return sqlFormat(sql, category, message);
} }
private static String formatByCommand(String category) {
private String sqlFormat(String sql, String category, String message) { return NEW_LINE
if(sql.trim().isEmpty()) { + "Execute Command : "
return ""; + NEW_LINE
+ NEW_LINE
+ TAP
+ category
+ NEW_LINE
+ NEW_LINE
+ "----------------------------------------------------------------------------------------------------";
} }
private String formatBySql(String sql, String category) {
String sqlComment = null; if (isStatementDDL(sql, category)) {
if(Objects.equals(Category.STATEMENT.getName(), category)) { return NEW_LINE
sql = sql.trim().toLowerCase(Locale.ROOT); + "Execute DDL : "
if(sql.startsWith("/*") && sql.contains("*/")){ + NEW_LINE
sqlComment = sql.substring(0, sql.indexOf("*/")+2); + FormatStyle.DDL
sql = sql.substring(sqlComment.length()).trim();
}
if(sql.startsWith("create") || sql.startsWith("alter") || sql.startsWith("comment") || sql.startsWith("drop")) {
sql = FormatStyle.DDL
.getFormatter() .getFormatter()
.format(sql); .format(sql);
} } return NEW_LINE
else { + "Execute DML : "
sql = FormatStyle.BASIC + NEW_LINE
+ FormatStyle.BASIC
.getFormatter() .getFormatter()
.format(sql); .format(sql);
} }
private String getAdditionalMessages(long elapsed) {
return NEW_LINE
+ NEW_LINE
+ String.format("Execution Time: %s ms", elapsed)
+ NEW_LINE
+ "----------------------------------------------------------------------------------------------------";
} }
private boolean isStatementDDL(String sql, String category) {
return new StringBuilder().append("\n") return isStatement(category) && isDDL(sql.trim().toLowerCase(Locale.ROOT));
.append("----------------------------------------------------------------------------------------------------") }
.append(sqlComment == null ? "" : "\n" + sqlComment) private boolean isStatement(String category) {
.append(sql) return Category.STATEMENT.getName().equals(category);
.append(message) }
.append("----------------------------------------------------------------------------------------------------") private boolean isDDL(String lowerSql) {
.toString(); return lowerSql.startsWith(CREATE)
|| lowerSql.startsWith(ALTER)
|| lowerSql.startsWith(DROP)
|| lowerSql.startsWith(COMMENT);
} }
} }
Loading…
Cancel
Save