-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix : 윈도우에서 makeGitHooksExecutable 에러 fix * build: p6spy 의존성 추가 * feat: application-jpa에 p6spy 로깅 추가 * feat: query formatter, config 추가 * feat: controller부터 추적되도록 stack trace filter 수정 * feat: 상수명 변경 --------- Co-authored-by: wjdwnsdnjs13 <[email protected]>
- Loading branch information
1 parent
24bc4b2
commit f088444
Showing
6 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
70 changes: 70 additions & 0 deletions
70
infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.depromeet.spot.jpa.common; | ||
|
||
import java.util.Locale; | ||
|
||
import org.hibernate.engine.jdbc.internal.FormatStyle; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.p6spy.engine.logging.Category; | ||
import com.p6spy.engine.spy.appender.MessageFormattingStrategy; | ||
|
||
@Configuration | ||
public class P6spySqlFormatter implements MessageFormattingStrategy { | ||
|
||
private static String ROOT_PACKAGE = "org.depromeet.spot"; | ||
private static String P6SPY_PACKAGE = "org.depromeet.spot.jpa.common.P6spySqlFormatter"; | ||
|
||
@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] | took %d ms | connectionId %d | %s | %s", | ||
category, elapsed, connectionId, filterStack(), formatSql(category, sql)); | ||
} | ||
|
||
private String formatSql(String category, String sql) { | ||
if (isNotEmpty(sql) && isStatement(category)) { | ||
String trimmedSQL = sql.trim().toLowerCase(Locale.ROOT); | ||
if (isDDL(trimmedSQL)) { | ||
return FormatStyle.DDL.getFormatter().format(sql); | ||
} | ||
return FormatStyle.BASIC.getFormatter().format(sql); | ||
} | ||
return sql; | ||
} | ||
|
||
// 일반적인 쿼리인지 판단 | ||
// 트랜잭션 커밋, 롤백 등 쿼리가 아닌 작업은 포맷팅하지 않는다. | ||
private boolean isStatement(String category) { | ||
return Category.STATEMENT.getName().equals(category); | ||
} | ||
|
||
private boolean isNotEmpty(String sql) { | ||
return sql != null && !sql.trim().isEmpty(); | ||
} | ||
|
||
private boolean isDDL(String sql) { | ||
return (sql.startsWith("create")) || sql.startsWith("alter") || sql.startsWith("comment"); | ||
} | ||
|
||
private String filterStack() { | ||
StackTraceElement[] stackTraces = new Throwable().getStackTrace(); | ||
StringBuilder sb = new StringBuilder(); | ||
int order = 1; | ||
|
||
for (StackTraceElement element : stackTraces) { | ||
String trace = element.toString(); | ||
if (trace.startsWith(ROOT_PACKAGE) && !trace.contains(P6SPY_PACKAGE)) { | ||
sb.append("\n\t\t").append(order++).append(".").append(trace); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/config/P6spyConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.depromeet.spot.jpa.config; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
|
||
import org.depromeet.spot.jpa.common.P6spySqlFormatter; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.p6spy.engine.spy.P6SpyOptions; | ||
|
||
@Configuration | ||
public class P6spyConfig { | ||
|
||
@PostConstruct | ||
public void setLogMessageFormat() { | ||
P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spySqlFormatter.class.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,8 @@ spring: | |
console: | ||
enabled: true | ||
path: /h2-console | ||
|
||
decorator: | ||
datasource: | ||
p6spy: | ||
enable-logging: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters