|
| 1 | +package org.layer.aop; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 6 | +import org.aspectj.lang.annotation.Around; |
| 7 | +import org.aspectj.lang.annotation.Aspect; |
| 8 | +import org.springframework.security.core.Authentication; |
| 9 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | +import org.springframework.util.StopWatch; |
| 12 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 13 | +import org.springframework.web.context.request.RequestContextHolder; |
| 14 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 15 | + |
| 16 | +import jakarta.servlet.http.HttpServletRequest; |
| 17 | +import lombok.extern.log4j.Log4j2; |
| 18 | + |
| 19 | +@Aspect |
| 20 | +@Component |
| 21 | +@Log4j2 |
| 22 | +public class ExecutionLoggingAop { |
| 23 | + |
| 24 | + // 모든 layer-api 모듈 내의 controller package에 존재하는 클래스 |
| 25 | + @Around("execution(* org.layer.domain..controller..*(..))") |
| 26 | + public Object logExecutionTrace(ProceedingJoinPoint pjp) throws Throwable { |
| 27 | + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); |
| 28 | + RequestMethod httpMethod = RequestMethod.valueOf(request.getMethod()); |
| 29 | + |
| 30 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 31 | + Long memberId = null; |
| 32 | + if (!authentication.getPrincipal().equals("anonymousUser")) { |
| 33 | + memberId = (Long) authentication.getPrincipal(); |
| 34 | + } |
| 35 | + |
| 36 | + String className = pjp.getSignature().getDeclaringType().getSimpleName(); |
| 37 | + String methodName = pjp.getSignature().getName(); |
| 38 | + String task = className + "." + methodName; |
| 39 | + |
| 40 | + log.info("[Call Method] " + httpMethod + ": " + task + " | Request userId=" + memberId); |
| 41 | + |
| 42 | + Object[] paramArgs = pjp.getArgs(); |
| 43 | + String loggingMessage = ""; |
| 44 | + int cnt = 1; |
| 45 | + for (Object object : paramArgs) { |
| 46 | + if (Objects.nonNull(object)) { |
| 47 | + String paramName = "[param" + cnt +"] " + object.getClass().getSimpleName(); |
| 48 | + String paramValue = " [value" + cnt +"] " + object; |
| 49 | + loggingMessage += paramName + paramValue + "\n"; |
| 50 | + cnt++; |
| 51 | + } |
| 52 | + } |
| 53 | + log.info("{}", loggingMessage); |
| 54 | + // 해당 클래스 처리 전의 시간 |
| 55 | + StopWatch sw = new StopWatch(); |
| 56 | + sw.start(); |
| 57 | + |
| 58 | + Object result = null; |
| 59 | + |
| 60 | + // 해당 클래스의 메소드 실행 |
| 61 | + try{ |
| 62 | + result = pjp.proceed(); |
| 63 | + } |
| 64 | + catch (Exception e){ |
| 65 | + log.warn("[ERROR] " + task + " 메서드 예외 발생 : " + e.getMessage()); |
| 66 | + throw e; |
| 67 | + } |
| 68 | + |
| 69 | + // 해당 클래스 처리 후의 시간 |
| 70 | + sw.stop(); |
| 71 | + long executionTime = sw.getTotalTimeMillis(); |
| 72 | + |
| 73 | + log.info("[ExecutionTime] " + task + " --> " + executionTime + " (ms)"); |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments