refactor: p6spy 로그 format 재정의

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

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