반응형
Spring AOP 메서드 추적 Flow

 

Spring AOP를 활용한 메소드 흐름을 로깅한 로직을 정리합니다.

Server.xml
<!-- spring aspect --> 
<bean id="loggingAspect" class="com.zz.framework.support.aspect.LoggingAspect" /> 
	<aop:config proxy-target-class="true"> 	
    <aop:aspect id="aspectLoggging" ref="loggingAspect"> 		
    	<aop:pointcut id="servicePointcut" expression="execution(* *..*.service.*Service*.*(..))" /> 		
    	<aop:around method="logAround" pointcut-ref="servicePointcut"  /> 	
    </aop:aspect> 
</aop:config>
 
LoggingAspect.java
public class LoggingAspect { 	
	private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
    
    public void logBefore(JoinPoint joinPoint) { 		
    	logger.debug("logBefore()"); 	
    }  	
    
    public void logAfter(JoinPoint joinPoint) { 		
    	logger.debug("logAfter()"); 	
    }  	
    
    /** 	 
    * 메소드 별 로그 앞뒤로 남기기 	 
    * @param joinPoint 	 
    * @return 	 
    * @throws Throwable 	 
    * @auther jp1020 2014. 1. 15. 	 
    */  	
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { 		
    	Object thisObj = joinPoint.getTarget(); 		
        String className = thisObj.getClass().getName(); 		
        Object[] args = joinPoint.getArgs(); 		
        long currentTime = System.currentTimeMillis();  		
      
        if(logger.isDebugEnabled()) { 			
        	logger.debug("================================================="); 			
        	logger.debug(">>>>>>>>> LOGGING START >>>>>>>>>>"); 			
            logger.debug("[class]:" + className); 			
            logger.debug("[method]:" + joinPoint.getSignature().getName() + "()"); 	
        }  		
        
        Object returnObj = joinPoint.proceed(); 		
        
        if(logger.isDebugEnabled()) { 			
        	logger.debug("[class]:" + className); 			
            logger.debug("[method]:" + joinPoint.getSignature().getName() + "()"); 	
            logger.debug("[소요시간]: {}ms", new Object[]{(System.currentTimeMillis()-currentTime)}); 
            logger.debug(">>>>>>>>>> LOGGING END >>>>>>>>>>"); 			
            logger.debug("================================================="); 		
        } 		
        
        return returnObj; 	
    } 
            
}

 

페이지 호출시 Console창에 출력된 로그를 확인 합니다.
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():46]:================================================= 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():47]:>>>>>>>> LOGGING START >>>>>>> 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():48]:[class]:com.zz.board.business.board.service.impl.BoardServiceImpl 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():49]:[method]:listBoard() 
[2014-02-13 17:52:23] DEBUG [AbstractBeanFactory.doGetBean():246]:Returning cached instance of singleton bean 'loggingAspect'  

내용생략......... 

[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():55]:[class]:com.zz.board.business.board.service.impl.BoardServiceImpl 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():56]:[method]:listBoard() 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():57]:[소요시간]: 281ms 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():58]:>>>>>>>> LOGGING END >>>>>>>> 
[2014-02-13 17:52:23] DEBUG [LoggingAspect.logAround():59]:=================================================

 

 

반응형

+ Recent posts