From 8740a8c6fc16c96e0e0191b62d6f67da5d1a1113 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Thu, 12 Sep 2024 16:02:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JPA=20query=20=EB=A1=9C=EA=B9=85=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 --- .../xit/ens/core/aop/AccessLogAspect.java | 83 ++++++++++++------- src/main/resources/application-local.yml | 16 ++-- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/main/java/cokr/xit/ens/core/aop/AccessLogAspect.java b/src/main/java/cokr/xit/ens/core/aop/AccessLogAspect.java index 9c6efdf..55baa6e 100644 --- a/src/main/java/cokr/xit/ens/core/aop/AccessLogAspect.java +++ b/src/main/java/cokr/xit/ens/core/aop/AccessLogAspect.java @@ -1,36 +1,32 @@ package cokr.xit.ens.core.aop; -import cokr.xit.ens.core.aop.repository.AccessLogRepository; -import cokr.xit.ens.core.exception.EnsException; -import cokr.xit.ens.core.exception.code.EnsErrCd; -import cokr.xit.ens.core.monitor.slack.event.MonitorEvent; -import cokr.xit.ens.core.utils.CmmnUtil; -import cokr.xit.ens.core.utils.JwtUtil; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -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 java.io.*; +import java.util.*; +import java.util.regex.*; +import java.util.stream.*; + +import javax.servlet.http.*; + +import org.aspectj.lang.*; +import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import javax.servlet.http.HttpServletRequest; -import java.io.BufferedReader; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; +import org.springframework.boot.autoconfigure.condition.*; +import org.springframework.context.*; +import org.springframework.http.*; +import org.springframework.stereotype.*; +import org.springframework.util.*; +import org.springframework.web.context.request.*; + +import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.databind.*; + +import cokr.xit.ens.core.aop.repository.*; +import cokr.xit.ens.core.exception.*; +import cokr.xit.ens.core.exception.code.*; +import cokr.xit.ens.core.monitor.slack.event.*; +import cokr.xit.ens.core.utils.*; +import lombok.*; +import lombok.extern.slf4j.*; /** * @author (주)엑스아이티 개발팀 @@ -59,6 +55,12 @@ public class AccessLogAspect { @Value("${app.access.auth.exclude.uri}") private List EXCLUDE_URIS; + @Value("${app.jpa.logging.param:false}") + private boolean JPA_LOGGING_PARAM; + + @Value("${app.jpa.logging.result:false}") + private boolean JPA_LOGGING_RESULT; + @Pointcut("execution(* cokr.xit..presentation.*Controller.*(..))") public void presentationLayer() { } @@ -368,5 +370,26 @@ public class AccessLogAspect { return false; } + // FIXME: JPA 쿼리 로그 출력 코드 추가 + @ConditionalOnProperty(value = "app.jpa.logging.enabled", havingValue = "true", matchIfMissing = false) + @Around("execution(* org.springframework.data.repository.CrudRepository+.*(..))") + public Object logJpaQuery(ProceedingJoinPoint joinPoint) throws Throwable { + String methodName = joinPoint.getSignature().getDeclaringType().getSimpleName() + "." + joinPoint.getSignature().getName(); + Object[] args = joinPoint.getArgs(); + + if(JPA_LOGGING_PARAM) + log.info("Executing JPA query - {}\n args: {}", methodName, Arrays.toString(args)); + else + log.info("Executing JPA query - {}", methodName); + + long startTime = System.currentTimeMillis(); + Object result = joinPoint.proceed(); + long endTime = System.currentTimeMillis(); + if(JPA_LOGGING_RESULT) + log.info("JPA query - {} : executed in {} ms\n result: {}", methodName, (endTime - startTime), result); + else + log.info("JPA query - {} : executed in {} ms", methodName, (endTime - startTime)); + return result; + } } diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 9014d7b..3590eca 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -33,10 +33,10 @@ spring: jpa: database-platform: org.hibernate.dialect.Oracle10gDialect - show-sql: true #하이버네이트SQL 콘솔 출력 여부 - properties: - hibernate: - format_sql: true + # show-sql: true #하이버네이트SQL 콘솔 출력 여부 + # properties: + # hibernate: + # format_sql: true hibernate: ddl-auto: none @@ -71,4 +71,10 @@ contract: xit: mblpage: payinf: - host: ${xit.app.url}${xit.app.ctx} \ No newline at end of file + host: ${xit.app.url}${xit.app.ctx} + +app: + jpa: + logging: + param: true + result: true