package com.emato.cus.supervise.aop; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; /** * Created by Anyx on 2015/5/13. * 日志AOP */ @Aspect public class AspectWebLog { private static final Logger logger = LoggerFactory.getLogger(AspectWebLog.class); ObjectMapper mapper = new ObjectMapper(); /** * 方法开始前执行 * @param jp:JoinPoint * @return */ public void beforeAdvice(JoinPoint jp) { try { Object[] args = jp.getArgs(); StringBuffer sb = new StringBuffer(); String str = null; if (args[0] instanceof Map) { Map map = (Map) args[0]; str = mapper.writeValueAsString(map); }else{ str = mapper.writeValueAsString(args[0]); } logger.info("入参:{}", str); } catch (JsonProcessingException e) { e.printStackTrace(); } } /** * 方法返回后执行 * @param jp:JoinPoint * @param retValue:String 主体方法传递到通知方法的返回值 * @return */ public void afterReturningAdvice(JoinPoint jp, String retValue) { logger.info("出参:{}",retValue); } /** * 方法异常后执行 * @param jp:JoinPoint * @return */ public void afterThrowingAdvice(JoinPoint jp) { logger.info("异常结束后:{}"); } /** * 方法完后执行,无论成功还是异常 * @param jp:JoinPoint * @return */ public void afterAdvice(JoinPoint jp) { logger.info("执行结束后:{}"); } /** * 主体方法返回后将执行的通知方法 * @param pjp:ProceedingJoinPoint * @return */ public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable { long bftime = System.currentTimeMillis(); Object retVal = pjp.proceed(); double time =(double)(System.currentTimeMillis() - bftime) / 1000; logger.debug("执行时间:{}秒", time); return retVal; } }