SpringMvc使用aop进行请求和响应的日志打印

代码只简单实现了请求和响应的日志打印

1、导入相应的jar包
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aop</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.9</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>
2、在spring-mvc.xml中加入配置
<!--为@Aspect注解的类创建代理-->
<aop:aspectj-autoproxy />
3、编写aop实现类
@Aspect
@Component
public class ControllerAop {

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

    /**
     * 定义切换,controller下的所有方法
     */
    @Pointcut(value = "execution(* com.ssm.controller.*.*(..))")
    public void log() {}

    /**
     * 请求方法之前打印日志
     */
    @Before(value = "log()")
    public void doBefore(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String method = request.getMethod();
        String remoteAddr = request.getRemoteAddr();
        String url = request.getRequestURL().toString();
        Object[] args = joinPoint.getArgs();
        if (args.length == 0) {
            return;
        }
        Map<String, Object> params = new HashMap<>();
        for (Object arg : args) {
            if (arg instanceof BindingResult) {
                continue;
            }
            if (arg instanceof HttpSession) {
                continue;
            }
            // 如果参数中含有文件,则打印文件相关信息
            if (arg instanceof MultipartFile) {
                MultipartFile file = (MultipartFile) arg;
                params.put("file", getFileParam(file));
            } else if (arg instanceof MultipartFile[]) {
                MultipartFile[] files = (MultipartFile[]) arg;
                Map<String, Object> t = new HashMap<>();
                for (MultipartFile file : files) {
                    t.put("file", getFileParam(file));
                }
                params.put("files", t);
            } else {
                try {
                    params.put(arg.getClass().getSimpleName(), arg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            logger.info("<ip:{}> {} <{}> params=>{}", remoteAddr, method, url, JSON.toJSONString(params));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 打印响应参数
     */
    @AfterReturning(returning = "obj", pointcut = "log()")
    public void doAfterReturning(Object obj) {
        logger.info("response=>{}", obj.toString());
    }
    
	private Map<String, Object> getFileParam(MultipartFile file) {
        Map<String, Object> params = new HashMap<>();
        params.put("文件名", file.getOriginalFilename());
        params.put("文件类型", file.getContentType());
        params.put("文件大小", FileUtil.getFileSize(file.getSize()));
        return params;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值