From 12862e5f0175727f62b34630342aae2ee1c703de Mon Sep 17 00:00:00 2001 From: Cho Sangwook <82208159+Sangwook02@users.noreply.github.com> Date: Sat, 16 Dec 2023 16:03:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20api=20=ED=98=B8=EC=B6=9C=20=EC=8B=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=20=EA=B8=B0=EB=A1=9D=ED=95=98=EB=8A=94=20aop?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20(#291)=20(#292)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/logger/ApiLoggerAOP.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/com/newfit/reservation/common/logger/ApiLoggerAOP.java diff --git a/src/main/java/com/newfit/reservation/common/logger/ApiLoggerAOP.java b/src/main/java/com/newfit/reservation/common/logger/ApiLoggerAOP.java new file mode 100644 index 00000000..492abf46 --- /dev/null +++ b/src/main/java/com/newfit/reservation/common/logger/ApiLoggerAOP.java @@ -0,0 +1,57 @@ +package com.newfit.reservation.common.logger; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; + +@Aspect +@Component +@Slf4j +public class ApiLoggerAOP { + + @Around("execution(* com.newfit.reservation.domains..controller..*(..))") + public Object execute(ProceedingJoinPoint joinPoint) throws Throwable { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = requestAttributes.getRequest(); + + String headerName = getHeaderName(request); + beforeApiCall(joinPoint, request, headerName); + + try { + return joinPoint.proceed(); + } finally { + afterApiCall(request, headerName); + } + } + + private void beforeApiCall(ProceedingJoinPoint joinPoint, HttpServletRequest request, String headerName) { + if (headerName != null) { + log.info("request: method = {}, {} = {}", joinPoint.getSignature().getName(), + headerName, request.getHeader(headerName)); + } + } + + private String getHeaderName(HttpServletRequest request) { + if (request.getHeader("authority-id") != null) { + return "authority-id"; + } else if (request.getHeader("user-id") != null) { + return "user-id"; + } else if (request.getHeader("oauth-history-id") != null) { + return "oauth-history-id"; + } + + return null; + } + + private void afterApiCall(HttpServletRequest request, String headerName) { + if (headerName != null) { + log.info("response: {} = {}", headerName, request.getHeader(headerName)); + } + } +} \ No newline at end of file