Skip to content

Commit

Permalink
feat: api 호출 시 로그 기록하는 aop 추가 (#291) (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook02 authored Dec 16, 2023
1 parent 989d700 commit 12862e5
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 12862e5

Please sign in to comment.