SpringBoot 拦截请求打印日志

SpringBoot 拦截请求打印日志

java 复制代码
import lombok.extern.slf4j.Slf4j;

import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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 org.springframework.web.multipart.MultipartFile;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Slf4j
@Aspect
@Component
public class BusDealConf {

    @Around("@within(org.springframework.web.bind.annotation.RestController)" +
            "||@within(org.springframework.stereotype.Controller)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        log.info("----------------------------------------开始----------------------------------------");
        if (null != attributes) {
            HttpServletRequest request = attributes.getRequest();
            log.info("请求路径: {}", request.getRequestURI());
            log.info("请求方式: {}", request.getMethod());
            log.info("请求方法: {}", point.getSignature());
            log.info("请求参数: {}", JSONUtil.toJsonPrettyStr(getPara(point.getArgs())));
        }

        long t1 = System.currentTimeMillis();
        Object result = point.proceed(point.getArgs());
        long t2 = System.currentTimeMillis();
        //log.info("响应参数: {}", JSONUtil.toJsonPrettyStr(result));
        log.info("执行耗时: {}ms", t2 - t1);
        log.info("----------------------------------------结束----------------------------------------");
        return result;
    }

    private List<Object> getPara(Object[] objects) {
        return Arrays.stream(objects)
                .filter(obj -> !(obj instanceof MultipartFile)
                && !(obj instanceof HttpServletResponse)
                && !(obj instanceof HttpServletRequest)).collect(Collectors.toList());
    }

}
相关推荐
unique_perfect1 小时前
vue2与springboot实现deepseek打印机聊天
spring boot·websocket·ai·vue2·deepseek
哈哈老师啊2 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
气π4 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi
老华带你飞5 小时前
列车售票|基于springboot 列车售票系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
汝生淮南吾在北6 小时前
SpringBoot+Vue在线考试系统
vue.js·spring boot·后端·毕业设计·毕设
script.boy6 小时前
基于spring boot校园二手交易平台的设计与实现
java·spring boot·后端
猿与禅7 小时前
Spring Boot 4.0 完整核心特性及实践指南
java·spring boot·后端·spring·重大升级·springboot4.0
曲莫终7 小时前
SpringBoot使用AutoConfiguration自动配置Bean
spring boot
E***U9458 小时前
从新手到入门:如何判断自己是否真的学会了 Spring Boot
数据库·spring boot·后端
invicinble8 小时前
javase-异常体系
开发语言·spring boot