-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
989d700
commit 12862e5
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/com/newfit/reservation/common/logger/ApiLoggerAOP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |