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

}
相关推荐
梦想恒远3 小时前
Spring Boot + LangChain4j 统计 token 成本:ChatModelListener 实测与单价口径
spring boot·后端
howard200510 小时前
Spring Boot 版新生报到可视化系统
spring boot·新生报到可视化系统
她的男孩12 小时前
子表只读列界面能改,保存却报不允许编辑:Forge Admin 把字段权限落到子表列
spring boot·低代码·工作流引擎
杨凯凡12 小时前
MySQL 死锁怎么排查?InnoDB 锁等待与 Spring Boot 整事务重试实战
数据库·spring boot·mysql
RuoyiOffice1 天前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
RuoyiOffice1 天前
KubeSphere 容器云实践:从管理 K8s 到建设企业 PaaS,还需要哪些能力
spring boot·kubernetes·kubesphere·jenkins·paas·微服务部署·ruoyi office
软件技术NINI1 天前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
Flynn Liang1 天前
LangChain4j 生产级工程骨架:12 个模块 + Docker 一键启动
java·spring boot·langchain·rag·langchain4j
RuoyiOffice1 天前
固定资产维修管理:从报修、审批到办结,原资产卡怎么回查?
spring boot·固定资产·ruoyi office·资产维修·维修审批·维修办结·资产履历
招风的黑耳1 天前
SpringBoot+Vue+MySQL 民宿在线预定平台 完整可运行版(源码+文档)
vue.js·spring boot·mysql