Skip to content

Commit

Permalink
feat: p6spy formatter 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
dgjinsu committed Jun 11, 2024
1 parent c2ed269 commit c25cc6f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions spy.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1717316396221|33|rollback|connection 2|url jdbc:mysql://localhost:3306/poison?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Seoul&characterEncoding=UTF-8||
38 changes: 38 additions & 0 deletions src/main/java/poison/global/config/P6SpySqlFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package poison.global.config;

import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.P6SpyOptions;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import jakarta.annotation.PostConstruct;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;

@Configuration
public class P6SpySqlFormatter implements MessageFormattingStrategy {

@PostConstruct
public void setLogMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(this.getClass().getName());
}

@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
return String.format("[%s] | %d ms | %s", category, elapsed, formatSql(category, sql));
}

private String formatSql(String category, String sql) {
if (sql != null && !sql.trim().isEmpty() && Category.STATEMENT.getName().equals(category)) {
String trimmedSQL = sql.trim().toLowerCase(Locale.ROOT);
if (trimmedSQL.startsWith("create") || trimmedSQL.startsWith("alter") || trimmedSQL.startsWith("comment")) {
sql = FormatStyle.DDL.getFormatter().format(sql);
} else {
sql = FormatStyle.BASIC.getFormatter().format(sql);
}
return sql;
}
return sql;
}
}
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ spring:
# properties:
# hibernate.format_sql: true
# dialect: org.hibernate.dialect.MySQL8InnoDBDialect
logging:
level:
org.hibernate.SQL: debug

0 comments on commit c25cc6f

Please sign in to comment.