parent
cda48816f5
commit
5d86c30b83
@ -0,0 +1,41 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### h2db ###
|
||||||
|
*.mv.db
|
||||||
|
*exchange*.xml
|
||||||
|
pack.xml
|
||||||
|
*.pid
|
||||||
|
|
||||||
|
logs/
|
@ -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();
|
|
||||||
int order = 1;
|
|
||||||
while(callStack.size() != 0) {
|
|
||||||
callStackBuilder.append("\n\t\t").append(order++).append(". ").append(callStack.pop());
|
|
||||||
}
|
|
||||||
|
|
||||||
String message = new StringBuilder().append("\n\n\tConnection ID: ").append(connectionId)
|
@Override
|
||||||
.append("\n\tExecution Time: ").append(elapsed).append(" ms\n")
|
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
|
||||||
.append("\n\tCall Stack (number 1 is entry point): ").append(callStackBuilder).append("\n")
|
if (sql.trim().isEmpty()) {
|
||||||
.toString();
|
return formatByCommand(category);
|
||||||
|
} return formatBySql(sql, category) + getAdditionalMessages(elapsed);
|
||||||
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
|
||||||
String sqlComment = null;
|
+ category
|
||||||
if(Objects.equals(Category.STATEMENT.getName(), category)) {
|
+ NEW_LINE
|
||||||
sql = sql.trim().toLowerCase(Locale.ROOT);
|
+ NEW_LINE
|
||||||
if(sql.startsWith("/*") && sql.contains("*/")){
|
+ "----------------------------------------------------------------------------------------------------";
|
||||||
sqlComment = sql.substring(0, sql.indexOf("*/")+2);
|
}
|
||||||
sql = sql.substring(sqlComment.length()).trim();
|
private String formatBySql(String sql, String category) {
|
||||||
}
|
if (isStatementDDL(sql, category)) {
|
||||||
if(sql.startsWith("create") || sql.startsWith("alter") || sql.startsWith("comment") || sql.startsWith("drop")) {
|
return NEW_LINE
|
||||||
sql = FormatStyle.DDL
|
+ "Execute DDL : "
|
||||||
.getFormatter()
|
+ NEW_LINE
|
||||||
.format(sql);
|
+ FormatStyle.DDL
|
||||||
}
|
.getFormatter()
|
||||||
else {
|
.format(sql);
|
||||||
sql = FormatStyle.BASIC
|
} return NEW_LINE
|
||||||
.getFormatter()
|
+ "Execute DML : "
|
||||||
.format(sql);
|
+ NEW_LINE
|
||||||
}
|
+ FormatStyle.BASIC
|
||||||
}
|
.getFormatter()
|
||||||
|
.format(sql);
|
||||||
return new StringBuilder().append("\n")
|
}
|
||||||
.append("----------------------------------------------------------------------------------------------------")
|
private String getAdditionalMessages(long elapsed) {
|
||||||
.append(sqlComment == null ? "" : "\n" + sqlComment)
|
return NEW_LINE
|
||||||
.append(sql)
|
+ NEW_LINE
|
||||||
.append(message)
|
+ String.format("Execution Time: %s ms", elapsed)
|
||||||
.append("----------------------------------------------------------------------------------------------------")
|
+ NEW_LINE
|
||||||
.toString();
|
+ "----------------------------------------------------------------------------------------------------";
|
||||||
|
}
|
||||||
|
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…
Reference in New Issue