Skip to content

Commit

Permalink
feat: P6Spy 쿼리 로깅 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
njw1204 committed May 23, 2023
1 parent 1209c50 commit 01175da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions ode-seoul/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'me.paulschwarz:spring-dotenv:3.0.0'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kr.njw.odeseoul.common.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 P6SpyConfig {
@PostConstruct
public void setLogMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spyPrettySqlFormatter.class.getName());
}

public static class P6spyPrettySqlFormatter implements MessageFormattingStrategy {
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared,
String sql, String url) {
sql = this.formatSql(category, sql);
return category + " completed in " + elapsed + " ms" + sql;
}

private String formatSql(String category, String sql) {
if (sql == null || sql.trim().equals("")) return "";

if (Category.STATEMENT.getName().equals(category)) {
String tmpsql = sql.trim().toLowerCase(Locale.ROOT);
if (tmpsql.startsWith("create") || tmpsql.startsWith("alter") || tmpsql.startsWith("comment")) {
sql = FormatStyle.DDL.getFormatter().format(sql);
} else {
sql = FormatStyle.BASIC.getFormatter().format(sql);
}
}

return sql;
}
}
}
8 changes: 4 additions & 4 deletions ode-seoul/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ management:
health:
show-details: always

logging:
level:
org.hibernate.SQL: debug
org.hibernate.orm.jdbc.bind: trace
decorator:
datasource:
p6spy:
enable-logging: true

ode-seoul:
security:
Expand Down

0 comments on commit 01175da

Please sign in to comment.