Spring Controller请求参数Log

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/xiong451823652/article/details/100587362
package com.example.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
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;
/**
 *@author: youthcool
 *@date: 2019/9/6
 *@description: 请求参数Log
 */
@Component
@Aspect
public class ControllerLog {

    private final static Logger logger= LoggerFactory.getLogger(ControllerLog.class);

    @Pointcut("execution(* com.example.controller..*(..))")
    public void controller() { }

    @Before("controller()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String url=request.getRequestURI();
        Object[] args=joinPoint.getArgs();
        if(logger.isDebugEnabled()){
             logger.debug("method: {},url: {},args:{}",request.getMethod(),url, JSON.toJSONString(args));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xiong451823652/article/details/100587362
今日推荐