springboot 配置切片记录日志

1、pom中配置依赖的aop包


	org.springframework.boot
	spring-boot-starter-aop

2、切片事件监控

@Aspect
@Component
public class WebLogAspect {
	private Logger logger = Logger.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.mplus.marketing.controller..*.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下请求内容
        logger.info("URL : " + request.getRequestURL().toString());
        logger.info("HTTP_METHOD : " + request.getMethod());
        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);
    }
   
}


注解@Pointcut配置要监控的方法,本例中文监控所有控制器的方法

注解@Before会在进入切片监控方法方法之前执行

注解@AfterReturning会在切片监控方法即将返回时执行



你可能感兴趣的:(Java,Web)