From db2dbf7452152804f33aaf30fb4124a5291eb704 Mon Sep 17 00:00:00 2001 From: wjdwnsdnjs13 <67488973+wjdwnsdnjs13@users.noreply.github.com> Date: Tue, 2 Jul 2024 23:50:29 +0900 Subject: [PATCH 1/6] =?UTF-8?q?fix=20:=20=EC=9C=88=EB=8F=84=EC=9A=B0?= =?UTF-8?q?=EC=97=90=EC=84=9C=20makeGitHooksExecutable=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index cfeccfd9..85044864 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,7 +62,11 @@ subprojects { } tasks.register("makeGitHooksExecutable") { - commandLine("chmod", "+x", "${rootProject.rootDir}/.git/hooks/pre-commit") + if (System.getProperty("os.name").contains("Windows")) { + commandLine("attrib", "+x", "${rootProject.rootDir}/.git/hooks/pre-commit") + } else { + commandLine("chmod", "+x", "${rootProject.rootDir}/.git/hooks/pre-commit") + } dependsOn("updateGitHooks") } From 4bda2b38f4fa2b9e56e37343fbba65498ea3beca Mon Sep 17 00:00:00 2001 From: EunjiShin Date: Wed, 3 Jul 2024 23:38:44 +0900 Subject: [PATCH 2/6] =?UTF-8?q?build:=20p6spy=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infrastructure/jpa/build.gradle.kts | 3 +++ versions.properties | 2 ++ 2 files changed, 5 insertions(+) diff --git a/infrastructure/jpa/build.gradle.kts b/infrastructure/jpa/build.gradle.kts index 06299a39..72d73e5c 100644 --- a/infrastructure/jpa/build.gradle.kts +++ b/infrastructure/jpa/build.gradle.kts @@ -5,6 +5,9 @@ dependencies { // spring implementation("org.springframework.boot:spring-boot-starter-data-jpa:_") + // p6spy + implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:_") + // h2 - DB (또는 도커) 세팅 후 사라질 예정,, runtimeOnly("com.h2database:h2") diff --git a/versions.properties b/versions.properties index c0dbde06..c3ce1b2e 100644 --- a/versions.properties +++ b/versions.properties @@ -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 + From 83d01f86b10f566cbb7257a74539e5b097b19205 Mon Sep 17 00:00:00 2001 From: EunjiShin Date: Wed, 3 Jul 2024 23:42:50 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20application-jpa=EC=97=90=20p6spy=20?= =?UTF-8?q?=EB=A1=9C=EA=B9=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infrastructure/jpa/src/main/resources/application-jpa.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/infrastructure/jpa/src/main/resources/application-jpa.yaml b/infrastructure/jpa/src/main/resources/application-jpa.yaml index 8f7afdca..c5b523ff 100644 --- a/infrastructure/jpa/src/main/resources/application-jpa.yaml +++ b/infrastructure/jpa/src/main/resources/application-jpa.yaml @@ -17,3 +17,8 @@ spring: console: enabled: true path: /h2-console + +decorator: + datasource: + p6spy: + enable-logging: true \ No newline at end of file From e2c73e2e61f632a17e8ab38d3a83f2210bf56741 Mon Sep 17 00:00:00 2001 From: EunjiShin Date: Thu, 4 Jul 2024 00:31:54 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20query=20formatter,=20config=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spot/jpa/common/P6spySqlFormatter.java | 70 +++++++++++++++++++ .../spot/jpa/config/P6spyConfig.java | 17 +++++ 2 files changed, 87 insertions(+) create mode 100644 infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java create mode 100644 infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/config/P6spyConfig.java diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java new file mode 100644 index 00000000..7d9fb2d6 --- /dev/null +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java @@ -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(); + } +} diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/config/P6spyConfig.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/config/P6spyConfig.java new file mode 100644 index 00000000..3209fa9b --- /dev/null +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/config/P6spyConfig.java @@ -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()); + } +} From a08a43eab79c26c23473850b8d14c66828399abb Mon Sep 17 00:00:00 2001 From: EunjiShin Date: Thu, 4 Jul 2024 00:47:48 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20controller=EB=B6=80=ED=84=B0=20?= =?UTF-8?q?=EC=B6=94=EC=A0=81=EB=90=98=EB=8F=84=EB=A1=9D=20stack=20trace?= =?UTF-8?q?=20filter=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jpa/src/main/java/org/depromeet/spot/jpa/common/.gitkeep | 0 .../java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/.gitkeep diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/.gitkeep b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java index 7d9fb2d6..c81c9dd4 100644 --- a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java @@ -11,8 +11,8 @@ @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"; + private static String JPA_PACKAGE = "org.depromeet.spot"; + private static String P6SPY_PACKAGE = "org.depromeet.spot.jpa.common.P6spySqlFormatter"; @Override public String formatMessage( From e69c6df52823223113f8ff5268aa867b57972256 Mon Sep 17 00:00:00 2001 From: EunjiShin Date: Thu, 4 Jul 2024 00:50:35 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EC=83=81=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java index c81c9dd4..67f75a3b 100644 --- a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/common/P6spySqlFormatter.java @@ -11,7 +11,7 @@ @Configuration public class P6spySqlFormatter implements MessageFormattingStrategy { - private static String JPA_PACKAGE = "org.depromeet.spot"; + private static String ROOT_PACKAGE = "org.depromeet.spot"; private static String P6SPY_PACKAGE = "org.depromeet.spot.jpa.common.P6spySqlFormatter"; @Override @@ -61,7 +61,7 @@ private String filterStack() { for (StackTraceElement element : stackTraces) { String trace = element.toString(); - if (trace.startsWith(JPA_PACKAGE) && !trace.contains(P6SPY_PACKAGE)) { + if (trace.startsWith(ROOT_PACKAGE) && !trace.contains(P6SPY_PACKAGE)) { sb.append("\n\t\t").append(order++).append(".").append(trace); } }