Spring AOP 로그 기록
                                            
 2719 단어  자바
                    
package com.config;
import java.util.Date;
/**
 * AOP       
 */
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.bean.InfoLogBean;
import com.service.LogService;
import com.util.DateUtil;
@Aspect
@Component
public class WebLogAspect {
	
	@Autowired
	private LogService logService;
	
	//     
	private String startTime;
        //    
	private String endtime;
	
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
    
    @Before(value="execution(public * com.controller..*.*(..))") 
    public void doBefore(JoinPoint joinPoint) {
    	startTime = DateUtil.getDateTimeHelper(new Date());
    }
    
    /**
     *       
     * @param joinPoint
     * @param result
     */
    @AfterReturning(returning = "result", value="execution(public * com.controller..*.*(..))") 
    public void afterReturnMethod(JoinPoint joinPoint,Object result) { 
    	endtime = DateUtil.getDateTimeHelper(new Date());
    	String code=joinPoint.getSignature().getName();
    	String req=JSON.toJSONString(joinPoint.getArgs());
    	String resp=JSON.toJSONString(result);
    	InfoLogBean lb=new InfoLogBean();
    	lb.setAddName(code);
    	lb.setReqJson(req);
    	lb.setRespJson(resp);
    	lb.setExpError("0");
    	lb.setStatusCD("0");
    	lb.setCreateDate(startTime);
    	lb.setEndDate(endtime);
        logService.addLog(lb);
    }
    
    /**
     *       
     * @param joinPoint
     * @param ex
     */
	@AfterThrowing(value = "execution(public * com.controller..*.*(..))", throwing = "ex")
	public void afterThrowing(JoinPoint joinPoint, Exception ex) {
		endtime = DateUtil.getDateTimeHelper(new Date());
		String code = joinPoint.getSignature().getName();
		String req = JSON.toJSONString(joinPoint.getArgs());
		String resp = JSON.toJSONString("'excause:'"+ ex.toString() + ";  exMessage:" + ex.getMessage());
		InfoLogBean lb = new InfoLogBean();
		lb.setAddName(code);
		lb.setReqJson(req);
		lb.setRespJson(resp);
		lb.setRespJson(resp);
		lb.setExpError("1");
		lb.setStatusCD("1");
		lb.setCreateDate(startTime);
    	lb.setEndDate(endtime);
		logService.addLog(lb);
		logger.error("AOP:WebLogAspect.afterThrowing method, error message:"+ ex.toString());
	}
    
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.