-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BSVR-55] P6spy를 이용한 쿼리 로그 설정 #12
Changes from 5 commits
db2dbf7
36a4c99
4bda2b3
83d01f8
e2c73e2
a08a43e
e69c6df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 JPA_PACKAGE = "org.depromeet.spot.jpa"; | ||
private static String P6SPY_PACKAGE = "org.depromeet.spot.jpa.config.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(JPA_PACKAGE) && !trace.contains(P6SPY_PACKAGE)) { | ||
sb.append("\n\t\t").append(order++).append(".").append(trace); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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()); | ||
} | ||
} |
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 | ||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enable-logging을 true로 두면 p6spy가 동작해. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 확인! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ version.org.springframework.boot..spring-boot-starter-data-jpa=3.0.1 | |
|
||
version.org.springdoc..springdoc-openapi-starter-webmvc-ui=2.5.0 | ||
|
||
version.com.github.gavlyukovskiy..p6spy-spring-boot-starter=1.9.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.9.0버전부터 spring boot3 지원! 업그레이드는 되는데, 다운그레이드는 하지 않도록 주의 필요~~ |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
차례대로