feat(common): p6spy 모듈 추가

- sql 쿼리 문을 위한 p6spy 모듈 추가
This commit is contained in:
bum12ark
2022-02-09 17:53:42 +09:00
parent 07fe4f9c27
commit 3faccb39ad
20 changed files with 472 additions and 31 deletions

View File

@@ -37,6 +37,8 @@ dependencies {
/*implementation 'org.springframework.boot:spring-boot-starter-security'*/
/*implementation 'org.springframework.cloud:spring-cloud-starter-config'*/
/*implementation 'org.springframework.kafka:spring-kafka'*/
// https://mvnrepository.com/artifact/com.github.gavlyukovskiy/p6spy-spring-boot-starter
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,16 @@
package com.justpickup.discountservice.global.config;
import com.justpickup.discountservice.global.p6spy.CustomP6spySqlFormat;
import com.p6spy.engine.spy.P6SpyOptions;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class P6spyConfiguration {
@PostConstruct
public void setMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(CustomP6spySqlFormat.class.getName());
}
}

View File

@@ -0,0 +1,67 @@
package com.justpickup.discountservice.global.p6spy;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale;
import java.util.Stack;
public class CustomP6spySqlFormat implements MessageFormattingStrategy {
// 표기에 허용되는 filter
private String ALLOW_FILTER = "com.justpickup";
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
if(sql.trim().isEmpty()) { // sql 이 없다면 출력하지 않아도 됨
return "";
}
// stack 을 구성하는 Format을 만든다
return sql + createStack(connectionId, elapsed);
}
private String formatSql(String category,String sql) {
if(sql ==null || sql.trim().equals("")) return sql;
// Only format Statement, distinguish DDL And DML
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;
}
// stack 콘솔 표기
private String createStack(int connectionId, long elapsed) {
Stack<String> callStack = new Stack<>();
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String trace = stackTraceElement.toString();
// trace 항목을 보고 내게 맞는 것만 필터
if(trace.startsWith(ALLOW_FILTER)) {
callStack.push(trace);
}
}
StringBuffer sb = new StringBuffer();
int order = 1;
while (callStack.size() != 0) {
sb.append("\n\t\t" + (order++) + "." + callStack.pop());
}
return new StringBuffer().append("\n\n\tConnection ID:").append(connectionId)
.append(" | Execution Time:").append(elapsed).append(" ms\n")
.append("\n\tExecution Time:").append(elapsed).append(" ms\n")
.append("\n\tCall Stack :").append(sb).append("\n")
.append("\n--------------------------------------")
.toString();
}
}

View File

@@ -12,15 +12,13 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: validate
generate-ddl: true
open-in-view: false
properties:
hibernate:
format_sql: true
show_sql: true
open-in-view: false
default_batch_fetch_size: 1000
eureka:
client:
service-url:
@@ -29,7 +27,11 @@ eureka:
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
level:
com.justpickup: DEBUG
org.hibernate.SQL: debug
# jpa query, parameter 로그 (p6spy)
decorator.datasource.p6spy:
enable-logging: true

View File

@@ -37,6 +37,8 @@ dependencies {
/*implementation 'org.springframework.boot:spring-boot-starter-security'*/
/*implementation 'org.springframework.cloud:spring-cloud-starter-config'*/
/*implementation 'org.springframework.kafka:spring-kafka'*/
// https://mvnrepository.com/artifact/com.github.gavlyukovskiy/p6spy-spring-boot-starter
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,16 @@
package com.justpickup.notificationservice.global.config;
import com.justpickup.notificationservice.global.p6spy.CustomP6spySqlFormat;
import com.p6spy.engine.spy.P6SpyOptions;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class P6spyConfiguration {
@PostConstruct
public void setMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(CustomP6spySqlFormat.class.getName());
}
}

View File

@@ -0,0 +1,67 @@
package com.justpickup.notificationservice.global.p6spy;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale;
import java.util.Stack;
public class CustomP6spySqlFormat implements MessageFormattingStrategy {
// 표기에 허용되는 filter
private String ALLOW_FILTER = "com.justpickup";
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
if(sql.trim().isEmpty()) { // sql 이 없다면 출력하지 않아도 됨
return "";
}
// stack 을 구성하는 Format을 만든다
return sql + createStack(connectionId, elapsed);
}
private String formatSql(String category,String sql) {
if(sql ==null || sql.trim().equals("")) return sql;
// Only format Statement, distinguish DDL And DML
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;
}
// stack 콘솔 표기
private String createStack(int connectionId, long elapsed) {
Stack<String> callStack = new Stack<>();
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String trace = stackTraceElement.toString();
// trace 항목을 보고 내게 맞는 것만 필터
if(trace.startsWith(ALLOW_FILTER)) {
callStack.push(trace);
}
}
StringBuffer sb = new StringBuffer();
int order = 1;
while (callStack.size() != 0) {
sb.append("\n\t\t" + (order++) + "." + callStack.pop());
}
return new StringBuffer().append("\n\n\tConnection ID:").append(connectionId)
.append(" | Execution Time:").append(elapsed).append(" ms\n")
.append("\n\tExecution Time:").append(elapsed).append(" ms\n")
.append("\n\tCall Stack :").append(sb).append("\n")
.append("\n--------------------------------------")
.toString();
}
}

View File

@@ -12,12 +12,11 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: validate
generate-ddl: true
open-in-view: false
properties:
hibernate:
format_sql: true
show_sql: true
open-in-view: false
default_batch_fetch_size: 1000
eureka:
@@ -28,7 +27,11 @@ eureka:
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
level:
com.justpickup: DEBUG
org.hibernate.SQL: debug
# jpa query, parameter 로그 (p6spy)
decorator.datasource.p6spy:
enable-logging: true

View File

@@ -36,6 +36,9 @@ dependencies {
/*implementation 'org.springframework.boot:spring-boot-starter-security'*/
/*implementation 'org.springframework.cloud:spring-cloud-starter-config'*/
/*implementation 'org.springframework.kafka:spring-kafka'*/
// https://mvnrepository.com/artifact/com.github.gavlyukovskiy/p6spy-spring-boot-starter
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,16 @@
package com.justpickup.orderservice.global.config;
import com.justpickup.orderservice.global.p6spy.CustomP6spySqlFormat;
import com.p6spy.engine.spy.P6SpyOptions;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class P6spyConfiguration {
@PostConstruct
public void setMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(CustomP6spySqlFormat.class.getName());
}
}

View File

@@ -0,0 +1,67 @@
package com.justpickup.orderservice.global.p6spy;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale;
import java.util.Stack;
public class CustomP6spySqlFormat implements MessageFormattingStrategy {
// 표기에 허용되는 filter
private String ALLOW_FILTER = "com.justpickup";
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
if(sql.trim().isEmpty()) { // sql 이 없다면 출력하지 않아도 됨
return "";
}
// stack 을 구성하는 Format을 만든다
return sql + createStack(connectionId, elapsed);
}
private String formatSql(String category,String sql) {
if(sql ==null || sql.trim().equals("")) return sql;
// Only format Statement, distinguish DDL And DML
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;
}
// stack 콘솔 표기
private String createStack(int connectionId, long elapsed) {
Stack<String> callStack = new Stack<>();
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String trace = stackTraceElement.toString();
// trace 항목을 보고 내게 맞는 것만 필터
if(trace.startsWith(ALLOW_FILTER)) {
callStack.push(trace);
}
}
StringBuffer sb = new StringBuffer();
int order = 1;
while (callStack.size() != 0) {
sb.append("\n\t\t" + (order++) + "." + callStack.pop());
}
return new StringBuffer().append("\n\n\tConnection ID:").append(connectionId)
.append(" | Execution Time:").append(elapsed).append(" ms\n")
.append("\n\tExecution Time:").append(elapsed).append(" ms\n")
.append("\n\tCall Stack :").append(sb).append("\n")
.append("\n--------------------------------------")
.toString();
}
}

View File

@@ -7,11 +7,11 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: validate
generate-ddl: true
show-sql: true
format_sql: true
open-in-view: false
properties:
hibernate:
default_batch_fetch_size: 1000
datasource:
@@ -29,7 +29,11 @@ eureka:
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
level:
com.justpickup: DEBUG
org.hibernate.SQL: debug
# jpa query, parameter 로그 (p6spy)
decorator.datasource.p6spy:
enable-logging: true

View File

@@ -36,6 +36,8 @@ dependencies {
/*implementation 'org.springframework.boot:spring-boot-starter-security'*/
/*implementation 'org.springframework.cloud:spring-cloud-starter-config'*/
/*implementation 'org.springframework.kafka:spring-kafka'*/
// https://mvnrepository.com/artifact/com.github.gavlyukovskiy/p6spy-spring-boot-starter
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,16 @@
package com.justpickup.storeservice.global.config;
import com.justpickup.storeservice.global.p6spy.CustomP6spySqlFormat;
import com.p6spy.engine.spy.P6SpyOptions;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class P6SpyConfiguration {
@PostConstruct
public void setMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(CustomP6spySqlFormat.class.getName());
}
}

View File

@@ -0,0 +1,67 @@
package com.justpickup.storeservice.global.p6spy;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale;
import java.util.Stack;
public class CustomP6spySqlFormat implements MessageFormattingStrategy {
// 표기에 허용되는 filter
private String ALLOW_FILTER = "com.justpickup";
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
if(sql.trim().isEmpty()) { // sql 이 없다면 출력하지 않아도 됨
return "";
}
// stack 을 구성하는 Format을 만든다
return sql + createStack(connectionId, elapsed);
}
private String formatSql(String category,String sql) {
if(sql ==null || sql.trim().equals("")) return sql;
// Only format Statement, distinguish DDL And DML
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;
}
// stack 콘솔 표기
private String createStack(int connectionId, long elapsed) {
Stack<String> callStack = new Stack<>();
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String trace = stackTraceElement.toString();
// trace 항목을 보고 내게 맞는 것만 필터
if(trace.startsWith(ALLOW_FILTER)) {
callStack.push(trace);
}
}
StringBuffer sb = new StringBuffer();
int order = 1;
while (callStack.size() != 0) {
sb.append("\n\t\t" + (order++) + "." + callStack.pop());
}
return new StringBuffer().append("\n\n\tConnection ID:").append(connectionId)
.append(" | Execution Time:").append(elapsed).append(" ms\n")
.append("\n\tExecution Time:").append(elapsed).append(" ms\n")
.append("\n\tCall Stack :").append(sb).append("\n")
.append("\n--------------------------------------")
.toString();
}
}

View File

@@ -7,12 +7,11 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: validate
generate-ddl: true
open-in-view: false
properties:
hibernate:
format_sql: true
show_sql: true
open-in-view: false
default_batch_fetch_size: 1000
datasource:
@@ -30,7 +29,11 @@ eureka:
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
level:
com.justpickup.storeservice: DEBUG
org.hibernate.SQL: debug
com.justpickup: DEBUG
# jpa query, parameter 로그 (p6spy)
decorator.datasource.p6spy:
enable-logging: true

View File

@@ -37,6 +37,8 @@ dependencies {
/*implementation 'org.springframework.boot:spring-boot-starter-security'*/
/*implementation 'org.springframework.cloud:spring-cloud-starter-config'*/
/*implementation 'org.springframework.kafka:spring-kafka'*/
// https://mvnrepository.com/artifact/com.github.gavlyukovskiy/p6spy-spring-boot-starter
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,16 @@
package com.justpickup.userservice.global.config;
import com.justpickup.userservice.global.p6spy.CustomP6spySqlFormat;
import com.p6spy.engine.spy.P6SpyOptions;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class P6spyConfiguration {
@PostConstruct
public void setMessageFormat() {
P6SpyOptions.getActiveInstance().setLogMessageFormat(CustomP6spySqlFormat.class.getName());
}
}

View File

@@ -0,0 +1,67 @@
package com.justpickup.userservice.global.p6spy;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import java.util.Locale;
import java.util.Stack;
public class CustomP6spySqlFormat implements MessageFormattingStrategy {
// 표기에 허용되는 filter
private String ALLOW_FILTER = "com.justpickup";
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
sql = formatSql(category, sql);
if(sql.trim().isEmpty()) { // sql 이 없다면 출력하지 않아도 됨
return "";
}
// stack 을 구성하는 Format을 만든다
return sql + createStack(connectionId, elapsed);
}
private String formatSql(String category,String sql) {
if(sql ==null || sql.trim().equals("")) return sql;
// Only format Statement, distinguish DDL And DML
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;
}
// stack 콘솔 표기
private String createStack(int connectionId, long elapsed) {
Stack<String> callStack = new Stack<>();
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String trace = stackTraceElement.toString();
// trace 항목을 보고 내게 맞는 것만 필터
if(trace.startsWith(ALLOW_FILTER)) {
callStack.push(trace);
}
}
StringBuffer sb = new StringBuffer();
int order = 1;
while (callStack.size() != 0) {
sb.append("\n\t\t" + (order++) + "." + callStack.pop());
}
return new StringBuffer().append("\n\n\tConnection ID:").append(connectionId)
.append(" | Execution Time:").append(elapsed).append(" ms\n")
.append("\n\tExecution Time:").append(elapsed).append(" ms\n")
.append("\n\tCall Stack :").append(sb).append("\n")
.append("\n--------------------------------------")
.toString();
}
}

View File

@@ -12,12 +12,11 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: validate
generate-ddl: true
open-in-view: false
properties:
hibernate:
format_sql: true
show_sql: true
open-in-view: false
default_batch_fetch_size: 1000
eureka:
@@ -28,7 +27,11 @@ eureka:
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
level:
com.justpickup: DEBUG
org.hibernate.SQL: debug
# jpa query, parameter 로그 (p6spy)
decorator.datasource.p6spy:
enable-logging: true