2020년 5월 12일 화요일

[Spring] Collecting logs with AOP

Logging can be a big help for debugging because it's easier to collect logs from specific methods that run with the AOP feature. This is because log collection makes it easy to know when the method was executed and what parameters were received.

jointPoint Interface

 Logging can be a big help for debugging because it's easier to collect logs from specific methods that run with the AOP feature. This is because log collection makes it easy to know when the method was executed and what parameters were received.

메서드설명
getSigniture()method with pointpoint applied (signature)
getSigniture().getName()Name of the method to which the pointpoint is applied
getSigniture().toLongString()Full name (output to package) of method with pointpoint applied
getSigniture().toShortString()Abbreviated name with pointpoint (only output method name)
getArgs()Parameters for method with pointpoint applied

Log processing using AOP


BoardAspect.java
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
...

@Component
@Aspect
public class BoardAspect {
 
 private static final Logger logger = LoggerFactory.getLogger(BoardAspect.class);
    
 // spring01/모든 폴더/service/이름이 ~Serviceimpl로 끝나는 클래스에 적용 
 @Around("execution(* com.example.spring01.*.service.*ServiceImpl.*(..))")
 public Object logging(ProceedingJoinPoint jp) throws Throwable {
    
        // 메서드가 실행되기 전
        logger.info("메서드 명: " + jp.getSignature().getName() + " 시작");
        logger.info("파라미터: " + Arrays.toString(jp.getArgs()));
        
        // 메서드 실행
        Object result = jointPoint.proceed();
  
        // 메서드가 실행된 후
        logger.info("메서드 명: " + jp.getSignature().getName() + " 종료");
        return result;
     }
 The AOP function specified a method for the BoardServiceImpl class that implemented the BoardService interface through an exit specifier. Before the method runs, the name is output by the getName method and the array of parameters returned by the getArgs() method is output by toString. After the method is executed, the name of the method is printed again and the method is terminated.

BoardVO
public class BoardVO {
 
    //프로퍼티 정의
    private int BoardId;
    private String BoardTitle;
            ...
    
    // getter, setter 설정
    public void setBoardId(int boardId) {
        this.BoardId = boardId;
    }
            ...
    
    @Override
    public String toString() {
        return "BoardVO [boardId=" + boardId + ", boardTitle=" + boardTitle + ", boardContent=" + boardContent
             + ", boardType=" + boardType + ", boardDate=" + boardDate + ", userId=" + userId + "]";
 }
}
 The VO object class to be passed over to the parameter of the method. Define the properties, define the getter/setters, and override the toString method as above to output the values of the properties. Automatically and easily create by pressing the Shortcut Alt + Shift + S and clicking Generate toString...

BoardController.java
@Controller
public clas BoardController {

    @Resource
    private BoardService boardService;

    @RequestMapping("/url")
    public String init(BoardVO boardVO) {
     // AOP가 적용된 selectBoardList 메서드
     List<BoardVO> homeList = homeService.selectBoardList(boardVO);
     
     return null;
    }
}
 Invoke the selectBoardList method with AOP feature on the controller. Run it and check the console window to see if any of the numerous logs defined above are printed.

결과
14:42:03.438 [http-nio-8079-exec-1] INFO com.example.project.aop.LogAspect - selectBoardList 시작
14:42:03.456 [http-nio-8079-exec-1] INFO com.example.project.aop.LogAspect - 파라미터: [Board

댓글 없음:

댓글 쓰기