package com.drj.springboot.aop;
import com.drj.springboot.controller.JspController;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
- // …
- // 佛祖保佑 永无BUG
- // 佛曰:
- // 写字楼里写字间,写字间里程序员;
- // 程序人员写程序,又拿程序换酒钱。
- // 酒醒只在网上坐,酒醉还来网下眠;
- // 酒醉酒醒日复日,网上网下年复年。
- // 但愿老死电脑间,不愿鞠躬老板前;
- // 奔驰宝马贵者趣,公交自行程序员。
- // 别人笑我忒疯癫,我笑自己命太贱;
- // 不见满街漂亮妹,哪个归得程序员?
- @author 戴容骏
- @date 2018/12/15 0015 16:38
*/
@Aspect
@Component
public class WebLogAspect {
private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Pointcut("execution(public * com.drj.springboot.controller.*.*(..))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
//url路径
logger.info("URL : " + request.getRequestURL().toString());
//请求方式
logger.info("HTTP_METHOD : " + request.getMethod());
//ip
logger.info("IP : " + request.getRemoteAddr());
//请求路径
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//请求参数
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
logger.info("RESPONSE : " + ret);
}
}