SpringBoot切面,打印参数日志

本文介绍了一种使用AOP(面向切面编程)进行日志记录的方法,通过Spring框架实现,详细展示了如何在不修改业务代码的情况下,对请求进行统一的日志记录,包括请求IP、URL、类名、方法名及参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据个人需要修改

具体代码如下:

package com.***.dfp.rtsync.common.aspect;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Description 日志切片
 *
 * @author Bob
 * @date 2020/8/17
 **/
@Component
@Aspect
@Order
@Slf4j
public class CompassAspect {

    /**
     * @description 切入点
     * @author Bob
     * @date 2020/8/25
     */
    @Pointcut("@within(org.springframework.web.bind.annotation.RestController) @within(org.springframework.stereotype.Controller)")
    public void logPointCut() {

    }

    /**
     * @description Advice方式
     * @author Bob
     * @date 2020/8/25
     */
    @Around("logPointCut()")
    public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String url = request.getRequestURI().toString();
        String ip = request.getRemoteAddr();
        String className = pjp.getTarget().getClass().getName();
        String methodName = pjp.getSignature().getName();
        log.info("##################ip:{},url:{},class:{},method:{},args:{}", ip, url, className, methodName, handlerParameter(pjp));
        try {
            long start = System.currentTimeMillis();
            Object result = pjp.proceed();
            long runTime = System.currentTimeMillis() - start;
            log.info("##################runTime:{},result:{}", runTime, JSON.toJSONString(result));
            return result;
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "系统开小差了";
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return "系统异常";
        }
    }

    /**
     * @description 参数组装
     * @author Bob
     * @date 2020/8/25
     */
    private String handlerParameter(ProceedingJoinPoint point) {
        StringBuilder stringBuilder = new StringBuilder();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Object[] args = point.getArgs();
        int i = 0;
        for (Object pojo : args) {
            stringBuilder.append(parameterNames[i]).append(":").append(pojo).append(",");
        }
        return stringBuilder.toString();
    }
}

参考:https://blog.youkuaiyun.com/lvhonglei1987/article/details/93874445

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值