-
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
refactoring: AOP μμ λ° μ΄λ Έν μ΄μ 컀μ€ν°λ§μ΄μ§ μ¬μμ±
- Loading branch information
Showing
2 changed files
with
120 additions
and
113 deletions.
There are no files selected for viewing
231 changes: 119 additions & 112 deletions
231
backEnd/src/main/java/com/quiz/ourclass/global/config/AopConfig.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 |
---|---|---|
@@ -1,112 +1,119 @@ | ||
//package com.quiz.ourclass.global.config; | ||
// | ||
//import com.quiz.ourclass.global.dto.ResultResponse; | ||
//import jakarta.servlet.http.HttpServletRequest; | ||
//import java.lang.reflect.Field; | ||
//import java.lang.reflect.Modifier; | ||
//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 org.springframework.http.ResponseEntity; | ||
//import org.springframework.stereotype.Component; | ||
//import org.springframework.web.context.request.RequestContextHolder; | ||
//import org.springframework.web.context.request.ServletRequestAttributes; | ||
// | ||
// | ||
///* | ||
// * 컨νΈλ‘€λ¬ λ³ μ€ν μκ° μ²΄ν¬ | ||
// * */ | ||
//@Aspect | ||
//@Component | ||
//@RequiredArgsConstructor | ||
//@Slf4j | ||
//public class AopConfig { | ||
// | ||
// private double beforeTime = 0L; | ||
// private double afterTime = 0L; | ||
// | ||
// @Pointcut("execution(* com..controller..*(..))") | ||
// public void ControllerMethod() { | ||
// } | ||
// | ||
// // 컨νΈλ‘€λ¬ λ΄μ λͺ¨λ 맀μλμ λνμ¬ Loggingμ μ€ννλ€. λ€λ§ μ΄λ Έν μ΄μ μ΄ λΆμ 맀μλλ μ€ννμ§ μλλ€. | ||
// @Around("ControllerMethod() && !@annotation(com.quiz.ourclass.global.config.annotation.LogExclusion)") | ||
// public Object logging(ProceedingJoinPoint pjp) throws Throwable { | ||
// // μμμκ° check | ||
// beforeTime = System.currentTimeMillis(); | ||
// // request νλΌλ―Έν° κ° κ°μ Έμ€κΈ° | ||
// Object[] args = pjp.getArgs(); | ||
// StringBuilder logMsg = new StringBuilder(); | ||
// // request κ° νμΈ | ||
// | ||
// log.info("-----------> REQUEST <Header>: {} \n <Body>: {}({}) ={}" | ||
// , getHeaderDetail() | ||
// , pjp.getSignature().getDeclaringTypeName() | ||
// , pjp.getSignature().getName() | ||
// , logMsg); | ||
// // κ²°κ³Ό νμΈ | ||
// ResponseEntity<ResultResponse<?>> result = (ResponseEntity<ResultResponse<?>>) pjp.proceed(); | ||
// // λμκ° check | ||
// afterTime = System.currentTimeMillis(); | ||
// if (result != null) { | ||
// log.info("-----------> RESPONSE : {}({}) = {} ({}ms)" | ||
// , pjp.getSignature().getDeclaringTypeName(), pjp.getSignature().getName(), | ||
// result.getBody().getData(), | ||
// (afterTime - beforeTime) / 1000.0); | ||
// } | ||
// return result; | ||
// } | ||
// | ||
// private String getObjectDetails(Object arg) { | ||
// StringBuilder details = new StringBuilder(); | ||
// // νλΌλ―Έν°μ ν΄λμ€ Reflection μ λ€κ³ μμ κ·Έ νλλ€μ νλ νλ κΉλ³Έλ€. | ||
// Field[] fields = arg.getClass().getDeclaredFields(); | ||
// for (Field field : fields) { | ||
// // μλ³ νμ μ΄ λμ§ νμΈνλ€. private final μμ μ μΈ μ νμΈμμ μ μΈνλ€. | ||
// // private final λ©€λ² λ³μλ 보μμ μ€λ₯κ° λλ―λ‘ μλ΅νλ€. | ||
// int modifiers = field.getModifiers(); | ||
// if (Modifier.isPrivate(modifiers) && Modifier.isFinal(modifiers)) { | ||
// continue; | ||
// } | ||
// // private λ©€λ² λ³μλ μ κ·Ό ν μ μλλ‘ νμ© | ||
// field.setAccessible(true); | ||
// try { | ||
// details.append((field.getName())).append("="); | ||
// details.append((field.get(arg))).append(", "); | ||
// } catch (IllegalAccessException e) { | ||
// throw new RuntimeException("νΉμ νλ μ κ·Όμ μ€ν¨νμ΅λλ€.", e); | ||
// } | ||
// } | ||
// | ||
// if (details.length() > 2) { | ||
// details.setLength(details.length() - 2); | ||
// } | ||
// | ||
// return details.toString(); | ||
// } | ||
// | ||
// // Header λ΄μ©μ νμΈνλ€. | ||
// private StringBuilder getHeaderDetail() { | ||
// | ||
// StringBuilder ans = new StringBuilder(); | ||
// | ||
// ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
// if (attrs != null) { | ||
// HttpServletRequest request = attrs.getRequest(); | ||
// ans.append(" μμ²μ£Όμ: ").append(request.getRequestURL().toString()) | ||
// .append(" μμ²Method: ").append(request.getMethod()) | ||
// .append(" IP μ£Όμ: ").append(request.getRemoteAddr()); | ||
// | ||
// | ||
// } else { | ||
// System.out.println("No HTTP request details available"); | ||
// } | ||
// | ||
// return ans; | ||
// } | ||
// | ||
//} | ||
// | ||
package com.quiz.ourclass.global.config; | ||
|
||
import com.quiz.ourclass.global.dto.ResultResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
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 org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
|
||
/* | ||
* 컨νΈλ‘€λ¬ λ³ μ€ν μκ° μ²΄ν¬ | ||
* */ | ||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AopConfig { | ||
|
||
private double beforeTime = 0L; | ||
private double afterTime = 0L; | ||
|
||
@Pointcut("execution(* com..controller..*(..))") | ||
public void ControllerMethod() { | ||
} | ||
|
||
// 컨νΈλ‘€λ¬ λ΄μ λͺ¨λ 맀μλμ λνμ¬ Loggingμ μ€ννλ€. λ€λ§ μ΄λ Έν μ΄μ μ΄ λΆμ 맀μλλ μ€ννμ§ μλλ€. | ||
@Around("@annotation(com.quiz.ourclass.global.config.annotation.LogInclude)") | ||
public Object logging(ProceedingJoinPoint pjp) throws Throwable { | ||
// μμμκ° check | ||
beforeTime = System.currentTimeMillis(); | ||
// request νλΌλ―Έν° κ° κ°μ Έμ€κΈ° | ||
Object[] args = pjp.getArgs(); | ||
StringBuilder logMsg = new StringBuilder(); | ||
// request κ° νμΈ | ||
for (Object arg : args) { | ||
logMsg.append(arg.getClass().getSimpleName()).append(" ["); | ||
logMsg.append(getObjectDetails(arg)).append("], "); | ||
} | ||
if (logMsg.length() > 2) { | ||
logMsg.setLength(logMsg.length() - 2); | ||
} | ||
|
||
log.info("-----------> REQUEST <Header>: {} \n <Body>: {}({}) ={}" | ||
, getHeaderDetail() | ||
, pjp.getSignature().getDeclaringTypeName() | ||
, pjp.getSignature().getName() | ||
, logMsg); | ||
// κ²°κ³Ό νμΈ | ||
ResponseEntity<ResultResponse<?>> result = (ResponseEntity<ResultResponse<?>>) pjp.proceed(); | ||
// λμκ° check | ||
afterTime = System.currentTimeMillis(); | ||
if (result != null) { | ||
log.info("-----------> RESPONSE : {}({}) = {} ({}ms)" | ||
, pjp.getSignature().getDeclaringTypeName(), pjp.getSignature().getName(), | ||
result.getBody().getData(), | ||
(afterTime - beforeTime) / 1000.0); | ||
} | ||
return result; | ||
} | ||
|
||
private String getObjectDetails(Object arg) { | ||
StringBuilder details = new StringBuilder(); | ||
// νλΌλ―Έν°μ ν΄λμ€ Reflection μ λ€κ³ μμ κ·Έ νλλ€μ νλ νλ κΉλ³Έλ€. | ||
Field[] fields = arg.getClass().getDeclaredFields(); | ||
for (Field field : fields) { | ||
// μλ³ νμ μ΄ λμ§ νμΈνλ€. private final μμ μ μΈ μ νμΈμμ μ μΈνλ€. | ||
// private final λ©€λ² λ³μλ 보μμ μ€λ₯κ° λλ―λ‘ μλ΅νλ€. | ||
int modifiers = field.getModifiers(); | ||
if (Modifier.isPrivate(modifiers) && Modifier.isFinal(modifiers)) { | ||
continue; | ||
} | ||
// private λ©€λ² λ³μλ μ κ·Ό ν μ μλλ‘ νμ© | ||
field.setAccessible(true); | ||
try { | ||
details.append((field.getName())).append("="); | ||
details.append((field.get(arg))).append(", "); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("νΉμ νλ μ κ·Όμ μ€ν¨νμ΅λλ€.", e); | ||
} | ||
} | ||
|
||
if (details.length() > 2) { | ||
details.setLength(details.length() - 2); | ||
} | ||
|
||
return details.toString(); | ||
} | ||
|
||
// Header λ΄μ©μ νμΈνλ€. | ||
private StringBuilder getHeaderDetail() { | ||
|
||
StringBuilder ans = new StringBuilder(); | ||
|
||
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
if (attrs != null) { | ||
HttpServletRequest request = attrs.getRequest(); | ||
ans.append(" μμ²μ£Όμ: ").append(request.getRequestURL().toString()) | ||
.append(" μμ²Method: ").append(request.getMethod()) | ||
.append(" IP μ£Όμ: ").append(request.getRemoteAddr()); | ||
|
||
|
||
} else { | ||
System.out.println("No HTTP request details available"); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
} | ||
|
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