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());
    }

}
相关推荐
Rain的Java大神之路7 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
软件技术NINI10 小时前
基于 SpringBoot 的酒店预定系统
java·spring boot·后端
软件技术NINI10 小时前
基于 Spring Boot 中国历史知识学习系统
spring boot·后端·学习
她的男孩12 小时前
会签选了候选角色,发起却报 nrOfInstances:Forge Admin 改成按人展开集合
spring boot·低代码·工作流引擎
学长毕设13 小时前
基于SpringBoot 宠物商城领养网站-附源码
spring boot·后端·宠物
AI开源软件14 小时前
SpringCloud+Nacos 业务协同落地:业务调用与集成中心远程管理如何分别配置
java·spring boot
BruceVan15 小时前
给 AI 配上 Context7:查文档更准,还更省 token
vue.js·spring boot·ai编程
FYKJ_20101 天前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
CoderYanger1 天前
Java EE 进阶:14.案例综合练习-博客系统(第二弹):博客详情+令牌技术
服务器·前端·spring boot·后端·spring·java-ee·github
FYKJ_20101 天前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计