맨땅에 헤딩하는 개바른자

[AOP] 실전예제 로그TRACE 본문

JAVA/AOP

[AOP] 실전예제 로그TRACE

앵낄낄 2023. 4. 18. 19:47
반응형

특정 메소드 단위로 어노테이션이 붙은 곳에서만 param 값에 대한 로그를 찍는 AOP를 적용해보는 글입니다.

실전코드 : https://github.com/ymwoo88/study-springboot-aop/tree/feature/스프링-AOP-실전코딩-로그trace

실전예제

  • 커스텀 어노테이션 생성
    • 로그 Trace에 사용 될 어노테이션으로 명칭을 그래로 Trace로 생성
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
}
  • @Aspect 컨피그 추가
    • 어노테이션 기반 joinPoint 생성
    @Slf4j
    @Aspect
    public class TraceAspect {
    
        @Before("@annotation(hello.aop.example.annotation.Trace)")
        public void doTrace(JoinPoint joinpoint) {
            Object[] args = joinpoint.getArgs();
            log.info("[trace] {} args = {}", joinpoint.getSignature(), args);
        }
    }
    
    • @Before 기능으로 @annotation을 포인트로 @Trace를 설정
    • joinpoint.getArgs()는 메소드 param 정보 모두 확인
    • joinpoint.getSignature 아래와 같은 리턴정보, 메소드명, 매개변수 정보 확인
      • void hello.aop.example.ExampleService.request(String)
  • 실전전용 
// ExampleService 
@Trace 
public void request(String itemId) { 
	exampleRepository.save(itemId); 
} 

// ExampleRepository 
@Trace public String save(String itemId) { 
	seq++; 
    if (seq % 5 == 0) { 
    	throw new IllegalStateException("예외발생"); 
    } return "OK"; 
}

// TestCode
@Slf4j
@SpringBootTest
@Import(TraceAspect.class)
public class ExampleTest {

    @Autowired
    ExampleService exampleService;

    @Test
    void test() {
        for (int i = 0; i < 5; i++) {
            log.info("client request i = {}", i);
            exampleService.request("data" + i);
        }
    }
}
  • 적용결과
2023-04-18 19:18:36.005  INFO 71017 --- [    Test worker] hello.aop.example.ExampleTest            : client request i = 0
2023-04-18 19:18:36.010  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] void hello.aop.example.ExampleService.request(String) args = [data0]
2023-04-18 19:18:36.016  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] String hello.aop.example.ExampleRepository.save(String) args = [data0]
2023-04-18 19:18:36.018  INFO 71017 --- [    Test worker] hello.aop.example.ExampleTest            : client request i = 1
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] void hello.aop.example.ExampleService.request(String) args = [data1]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] String hello.aop.example.ExampleRepository.save(String) args = [data1]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.ExampleTest            : client request i = 2
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] void hello.aop.example.ExampleService.request(String) args = [data2]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] String hello.aop.example.ExampleRepository.save(String) args = [data2]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.ExampleTest            : client request i = 3
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] void hello.aop.example.ExampleService.request(String) args = [data3]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] String hello.aop.example.ExampleRepository.save(String) args = [data3]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.ExampleTest            : client request i = 4
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] void hello.aop.example.ExampleService.request(String) args = [data4]
2023-04-18 19:18:36.019  INFO 71017 --- [    Test worker] hello.aop.example.aop.TraceAspect        : [trace] String hello.aop.example.ExampleRepository.save(String) args = [data4]

 

실제로 AOP를 적용해보니 정말 아무것도 아닌 것 처럼 느껴지네요

오늘 포스팅은 간략하게 끝마무리.

반응형

'JAVA > AOP' 카테고리의 다른 글

[AOP] 실전예제 Service단 파라미터 Null체크 사용기  (1) 2023.04.25
[AOP] this, target  (0) 2023.03.29
[AOP] 매개변수 전달  (0) 2023.03.29
[AOP] @annotation  (0) 2023.03.23
[AOP] @target @within  (0) 2023.03.23