diff --git a/module-domain/src/main/java/com/mile/common/log/P6spySqlFormat.java b/module-domain/src/main/java/com/mile/common/log/P6spySqlFormat.java new file mode 100644 index 00000000..d46f4b47 --- /dev/null +++ b/module-domain/src/main/java/com/mile/common/log/P6spySqlFormat.java @@ -0,0 +1,33 @@ +package com.mile.common.log; + +import com.p6spy.engine.logging.Category; +import com.p6spy.engine.spy.appender.MessageFormattingStrategy; +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.slf4j.MDC; + +import java.util.Locale; + +public class P6spySqlFormat implements MessageFormattingStrategy { + private final String MDC_KEY = "SQL_START"; + + @Override + public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { + sql = formatSql(category, sql); + return MDC.get(MDC_KEY) + "|" + sql; + } + + private String formatSql(String category, String sql) { + if (sql == null || sql.trim().equals("")) return sql; + + 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; + } +} \ No newline at end of file diff --git a/module-domain/src/main/java/com/mile/common/log/P6spySqlFormatConfig.java b/module-domain/src/main/java/com/mile/common/log/P6spySqlFormatConfig.java new file mode 100644 index 00000000..c3aeb1b6 --- /dev/null +++ b/module-domain/src/main/java/com/mile/common/log/P6spySqlFormatConfig.java @@ -0,0 +1,14 @@ +package com.mile.common.log; + +import com.p6spy.engine.spy.P6SpyOptions; +import jakarta.annotation.PostConstruct; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class P6spySqlFormatConfig { + + @PostConstruct + public void setLogMessageFormat() { + P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spySqlFormat.class.getName()); + } +} diff --git a/module-domain/src/main/java/com/mile/common/log/SqlFunctionLoggingModule.java b/module-domain/src/main/java/com/mile/common/log/SqlFunctionLoggingModule.java new file mode 100644 index 00000000..be5cb4ef --- /dev/null +++ b/module-domain/src/main/java/com/mile/common/log/SqlFunctionLoggingModule.java @@ -0,0 +1,35 @@ +package com.mile.common.log; + +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; + +/** + * from @sohyundoh + *
+ * SQL 진입점을 로깅하기 위한 AOP 클래스 + */ +@Aspect +@Component +@RequiredArgsConstructor(access = AccessLevel.PROTECTED) +@Slf4j +public class SqlFunctionLoggingModule { + private final String MDC_KEY = "SQL_START"; + + @Pointcut("execution(* com.mile.*.repository..*.*(..))") + public void sqlLoggingPoint() { + } + + @Around("sqlLoggingPoint()") + public Object putSqlStartingPoint(final ProceedingJoinPoint joinPoint) throws Throwable { + MDC.put(MDC_KEY, "[ QUERY START -> " + joinPoint.getSignature().toShortString() + "]"); + + return joinPoint.proceed(); + } +} diff --git a/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java b/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java index 43865d32..85b7fa5f 100644 --- a/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java +++ b/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java @@ -1,7 +1,6 @@ package com.mile.moim.repository; import com.mile.moim.domain.Moim; -import com.mile.post.domain.Post; import com.mile.writername.domain.WriterName; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository;