mport com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Slf4j
@Component
@Aspect
public class WebLogAop {
/**
* 配置切点,扫描controller下的所有方法
*/
@Pointcut("execution(public * com.a.b.controller..*(..))")
public void webLogAop() {
}
/**
* 在webLogApp方法执行之前执行,
* 也就是在到达切点前执行
* @param joinPoint
*/
@Before(value = "webLogAop()")
@Order(1)
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("===URL: " + request.getRequestURL().toString());
log.info("===HTTP_METHOD: " + request.getMethod());
log.info("===IP: " + request.getRemoteAddr());
log.info("===CLASS_METHOD: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
log.info("===ARGS: " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "webLogAop()")
@Order(2)
public void doAfterReturning(Object ret) {
log.info("===RESPONSE: " + JSON.toJSONString(ret));
}
}