From 896e52768d0ba72d5e53a202b9df8b84624f1fc3 Mon Sep 17 00:00:00 2001 From: Younglong Date: Tue, 9 Jan 2024 11:52:24 +0900 Subject: [PATCH 1/2] feat: log filter & aop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기존에 aop 있던 비즈니스 로직 전, 후 로깅 로직은 filter로 옮김 (실행 시점 및 세부 로그는 일부 달라짐) - aop에는 커스텀 어노테이션으로 로그 남기는 형태로 하나 만들어둠 - 실행시간 측정하는 용도의 커스텀 어노테이션 생성 --- .../java/com/kernel360/aop/LogAspect.java | 41 ------------- .../global/annotation/LogExecutionTime.java | 11 ++++ .../com/kernel360/global/aop/LogAspect.java | 24 ++++++++ .../kernel360/global/filter/LogFilter.java | 61 +++++++++++++++++++ 4 files changed, 96 insertions(+), 41 deletions(-) delete mode 100644 module-api/src/main/java/com/kernel360/aop/LogAspect.java create mode 100644 module-api/src/main/java/com/kernel360/global/annotation/LogExecutionTime.java create mode 100644 module-api/src/main/java/com/kernel360/global/aop/LogAspect.java create mode 100644 module-api/src/main/java/com/kernel360/global/filter/LogFilter.java diff --git a/module-api/src/main/java/com/kernel360/aop/LogAspect.java b/module-api/src/main/java/com/kernel360/aop/LogAspect.java deleted file mode 100644 index 1b4f05bb..00000000 --- a/module-api/src/main/java/com/kernel360/aop/LogAspect.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.kernel360.aop; - -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.*; -import org.springframework.stereotype.Component; - -import java.util.Arrays; - -@Slf4j -@Aspect -@Component -public class LogAspect { - - @Pointcut("within(*..*Controller)") - public void allController() {} - - @Before("allController()") - public void beforeRequest(JoinPoint joinPoint) { - log.info("###Start request {}", joinPoint.getSignature().toShortString()); - - Arrays.stream(joinPoint.getArgs()) - .map(arg -> arg != null ? "\t" + arg : "\tnull") - .forEach(log::info); - } - - @AfterReturning(pointcut = "allController()", returning = "returnValue") - public void afterReturningLogging(JoinPoint joinPoint, Object returnValue) { - log.info("###End request {}", joinPoint.getSignature().toShortString()); - - if (returnValue == null) return; - - log.info("###returnValue -> {}", returnValue); - } - - @AfterThrowing(pointcut = "allController()", throwing = "e") - public void afterThrowingLogging(JoinPoint joinPoint, Exception e) { - log.error("###Occured error in request {}", joinPoint.getSignature().toShortString()); - log.error("###e.getMessage() -> {}", e.getMessage()); - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/kernel360/global/annotation/LogExecutionTime.java b/module-api/src/main/java/com/kernel360/global/annotation/LogExecutionTime.java new file mode 100644 index 00000000..4086c6de --- /dev/null +++ b/module-api/src/main/java/com/kernel360/global/annotation/LogExecutionTime.java @@ -0,0 +1,11 @@ +package com.kernel360.global.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface LogExecutionTime { +} diff --git a/module-api/src/main/java/com/kernel360/global/aop/LogAspect.java b/module-api/src/main/java/com/kernel360/global/aop/LogAspect.java new file mode 100644 index 00000000..037e8951 --- /dev/null +++ b/module-api/src/main/java/com/kernel360/global/aop/LogAspect.java @@ -0,0 +1,24 @@ +package com.kernel360.global.aop; + +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +@Slf4j +@Aspect +@Component +public class LogAspect { + + @Around("@annotation(com.kernel360.global.annotation.LogExecutionTime)") + public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { + long start = System.currentTimeMillis(); + Object proceed = joinPoint.proceed(); + + long executionTime = System.currentTimeMillis() - start; + log.info("##### @LogExecutionTime ##### " + joinPoint.getSignature() + " executed in " + executionTime + "ms"); + + return proceed; + } +} \ No newline at end of file diff --git a/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java b/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java new file mode 100644 index 00000000..b41adf57 --- /dev/null +++ b/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java @@ -0,0 +1,61 @@ +package com.kernel360.global.filter; + +import jakarta.servlet.*; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; + +import java.io.IOException; + +@Slf4j +@Component +public class LogFilter implements Filter { + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + ContentCachingRequestWrapper req = new ContentCachingRequestWrapper((HttpServletRequest) request); + ContentCachingResponseWrapper res = new ContentCachingResponseWrapper((HttpServletResponse) response); + log.info("##### INIT URI: {}", req.getRequestURI()); + + chain.doFilter(req, res); + + // Request + StringBuilder reqHeaderValues = new StringBuilder(); + req.getHeaderNames().asIterator().forEachRemaining(headerKey -> { + String headerValue = req.getHeader(headerKey); + + reqHeaderValues + .append("[") + .append(headerKey) + .append(" : ") + .append(headerValue) + .append("] "); + }); + + String requestBody = new String(req.getContentAsByteArray()); + String uri = req.getRequestURI(); + String method = req.getMethod(); + log.info("##### REQUEST ##### uri: {}, method: {}, header: {}, body: {}", uri, method, reqHeaderValues, requestBody); + + // Response + StringBuilder resHeaderValues = new StringBuilder(); + res.getHeaderNames().forEach(headerKey -> { + String headerValue = res.getHeader(headerKey); + + resHeaderValues + .append("[") + .append(headerKey) + .append(" : ") + .append(headerValue) + .append("] "); + }); + + String responseBody = new String(res.getContentAsByteArray()); + log.info("##### RESPONSE ##### uri: {}, method: {}, header: {}, body: {}", uri, method, resHeaderValues, responseBody); + + res.copyBodyToResponse(); + } +} From fc2549f54cb68ccef557e047a348239a517dd0f2 Mon Sep 17 00:00:00 2001 From: Younglong Date: Tue, 9 Jan 2024 12:36:33 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20log=20filter=20&=20aop=20->=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 변수명 수정 --- .../kernel360/global/filter/LogFilter.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java b/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java index b41adf57..dfea3ef2 100644 --- a/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java +++ b/module-api/src/main/java/com/kernel360/global/filter/LogFilter.java @@ -15,19 +15,19 @@ public class LogFilter implements Filter { @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - ContentCachingRequestWrapper req = new ContentCachingRequestWrapper((HttpServletRequest) request); - ContentCachingResponseWrapper res = new ContentCachingResponseWrapper((HttpServletResponse) response); - log.info("##### INIT URI: {}", req.getRequestURI()); + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + ContentCachingRequestWrapper request = new ContentCachingRequestWrapper((HttpServletRequest) req); + ContentCachingResponseWrapper response = new ContentCachingResponseWrapper((HttpServletResponse) res); + log.info("##### INIT URI: {}", request.getRequestURI()); - chain.doFilter(req, res); + chain.doFilter(request, response); // Request - StringBuilder reqHeaderValues = new StringBuilder(); - req.getHeaderNames().asIterator().forEachRemaining(headerKey -> { - String headerValue = req.getHeader(headerKey); + StringBuilder requestHeaderValues = new StringBuilder(); + request.getHeaderNames().asIterator().forEachRemaining(headerKey -> { + String headerValue = request.getHeader(headerKey); - reqHeaderValues + requestHeaderValues .append("[") .append(headerKey) .append(" : ") @@ -35,17 +35,17 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha .append("] "); }); - String requestBody = new String(req.getContentAsByteArray()); - String uri = req.getRequestURI(); - String method = req.getMethod(); - log.info("##### REQUEST ##### uri: {}, method: {}, header: {}, body: {}", uri, method, reqHeaderValues, requestBody); + String requestBody = new String(request.getContentAsByteArray()); + String uri = request.getRequestURI(); + String method = request.getMethod(); + log.info("##### REQUEST ##### uri: {}, method: {}, header: {}, body: {}", uri, method, requestHeaderValues, requestBody); // Response - StringBuilder resHeaderValues = new StringBuilder(); - res.getHeaderNames().forEach(headerKey -> { - String headerValue = res.getHeader(headerKey); + StringBuilder responseHeaderValues = new StringBuilder(); + response.getHeaderNames().forEach(headerKey -> { + String headerValue = response.getHeader(headerKey); - resHeaderValues + responseHeaderValues .append("[") .append(headerKey) .append(" : ") @@ -53,9 +53,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha .append("] "); }); - String responseBody = new String(res.getContentAsByteArray()); - log.info("##### RESPONSE ##### uri: {}, method: {}, header: {}, body: {}", uri, method, resHeaderValues, responseBody); + String responseBody = new String(response.getContentAsByteArray()); + log.info("##### RESPONSE ##### uri: {}, method: {}, header: {}, body: {}", uri, method, responseHeaderValues, responseBody); - res.copyBodyToResponse(); + response.copyBodyToResponse(); } }