Framework/Spring Boot

[Spring Boot] Spring AOP를 활용한 Logging

꽁치_로그 2024. 4. 21. 15:43

기본적으로 함수 시작 전, 종료 후, 에러 발생 후 시점을 기준으로 자동으로 로그를 생성하도록 Aspect를 만들어서 설정할 수 있습니다.

execution() 안에서 로그를 자동으로 남길 범위를 정할 수 있습니다.

로깅이 필요하지 않은 함수들은 @NoLogging 어노테이션을 만들어서 처리할 수 있습니다.

(민감 정보를 가지고 있거나 로깅이 필요하지 않는 함수 등)

Advice 실행 시점 설명
@Before 함수 시작 전 함수가 속한 클래스 이름, 함수 이름, 함수로 들어온 파라미터(args) 등을 로그로 남깁니다.
@AfterReturning 함수 시작 후 함수가 속한 클래스 이름, 함수 이름, 함수가 반환한 값(result) 등을 로그로 남깁니다.
@AfterThrowing 에러 발생 후 함수가 속한 클래스 이름, 함수 이름, 에러 메세지(exception) 등을 로그로 남깁니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Component
@Aspect
@Slf4j
public class LogAspect {
   private static final String CLASS_LOG_FORMAT = "Class Name : ";
   private static final String METHOD_LOG_FORMAT = "Method Name : ";
   private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
 
   @Before("execution(* eegoba.exception..*(..)))" + "|| execution(* eegoba.controller..*(..)))" + "|| execution(* eegoba.service..*(..)))")
   public void beforeMethod(final JoinPoint joinPoint){
      HttpServletRequest request = ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes()) ? null : ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
 
      String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
      String name = joinPoint.getSignature().getName();
      String args = Arrays.toString(joinPoint.getArgs());
 
      StringBuilder logStringBuilder = new StringBuilder();
      String logInfo = logStringBuilder.append(CLASS_LOG_FORMAT + declaringTypeName).append(METHOD_LOG_FORMAT + name).append("args : " + args).toString();
 
      LOGGER.info("{}", logInfo);
   }
 
   @AfterReturning(value = "execution(* eegoba.exception..*(..)))" + "|| execution(* eegoba.controller..*(..)))" + "|| execution(* eegoba.service..*(..)))", returning = "result")
   public void afterMethod(final JoinPoint joinPoint, final Object result){
      HttpServletRequest request = ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes()) ? null : ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
 
      String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
      String name = joinPoint.getSignature().getName();
 
      StringBuilder logStringBuilder = new StringBuilder();
      String logInfo = logStringBuilder.append(CLASS_LOG_FORMAT + declaringTypeName).append(METHOD_LOG_FORMAT + name).toString();
 
      LOGGER.info("{}", logInfo);
   }
 
   @AfterThrowing(value = "execution(* eegoba.exception..*(..)))" + "|| execution(* eegoba.controller..*(..)))" + "|| execution(* eegoba.service..*(..)))", throwing = "exception")
   public void afterThrowing(final JoinPoint joinPoint, final Exception exception){
      HttpServletRequest request = ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes()) ? null : ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
 
      String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
      String name = joinPoint.getSignature().getName();
      String exceptionMessage = exception.getMessage();
 
      StringBuilder logStringBuilder = new StringBuilder();
      String logInfo = logStringBuilder.append(CLASS_LOG_FORMAT + declaringTypeName).append(METHOD_LOG_FORMAT + name).append("exceptionMessage : " + exceptionMessage).toString();
 
      LOGGER.info("{}", logInfo);
   }
   
}
 
cs
반응형