SpringBoot全局日志处理【出参,入参等】

本文介绍了一种使用自定义注解和AOP技术实现的方法级日志记录方案,详细展示了如何创建自定义注解SysLog,并通过SysLogAspect切面自动记录方法的入参、出参及执行耗时。

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

话不多说,直接上代码

package com.xxx.customAnnotations;

import java.lang.annotation.*;

/**
 * 日志处理(结合SysLogAspect.java使用,作用于方法上,自动记录方法的入参、出参等信息)
 *
 * @author : leims
 * @date : 2019-08-20 14:48
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
}
package com.xxx.customAnnotations;

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.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 日志记录切面(入参,出参,批次,耗时)
 *
 * @author : leims
 * @date : 2019-08-20 14:49
 */
@Slf4j
@Aspect
@Component
public class SysLogAspect {

    /**
     * 方法添加注解模式
     */
    @Pointcut("@annotation(com.xxx.customAnnotations.SysLog)")
    public void log() {
    }

    /**
     * 扫包模式(仅扫Controller)
     */
    @Pointcut("within(*com.xxx.controller.*.*)")
    public void scanerLog() {
    }

    /**
     * 自动记录方法日志(扫包模式)
     *
     * @param joinPoint 切入点
     * @return java.lang.Object 响应信息
     *
     * @author : She Leiming
     * @date : 2019-08-20 15:02:35
     */
    @Around(value = "scanerLog()")
    public Object scanerLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();

        String className = joinPoint.getTarget().getClass().getName();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = SingleLocalVariableTableParameterNameDiscoverer.getInstance().getParameterNames(method);
        Object[] requestParams = joinPoint.getArgs();
        if (null != parameterNames) {
            for (int i = 0; i < parameterNames.length; i++) {
                requestParams[i] = parameterNames[i] + " = " + requestParams[i];
            }
        }
        long batch = System.currentTimeMillis();

        log.info(":::::: ==> " + className + "." + method.getName() + "()");
        log.info(":::::: ==> Request Batch  : " + batch);
        log.info(":::::: ==> Request Params : " + JSON.toJSONString(requestParams));

        Object responseInfo = joinPoint.proceed();

        long end = System.currentTimeMillis();

        log.info(":::::: <== " + className + "." + method.getName() + "()");
        log.info(":::::: <== Request  Batch : " + batch);
        log.info(":::::: <== Time Consuming : " + (end - start) + " ms");
        log.info(":::::: <== Response  Info : " + JSON.toJSONString(responseInfo));

        return responseInfo;
    }

    /**
     * 加入注解自动记录方法日志(方法添加注解模式)
     *
     * @param joinPoint 切入点
     * @return java.lang.Object 响应信息
     *
     * @author : leims
     * @date : 2019-08-20 17:29:03
     */
    @Around(value = "log()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();

        String className = joinPoint.getTarget().getClass().getName();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = SingleLocalVariableTableParameterNameDiscoverer.getInstance().getParameterNames(method);
        Object[] requestParams = joinPoint.getArgs();
        if (null != parameterNames) {
            for (int i = 0; i < parameterNames.length; i++) {
                requestParams[i] = parameterNames[i] + " = " + requestParams[i];
            }
        }
        long batch = System.currentTimeMillis();

        log.info(":::::: ==> " + className + "." + method.getName() + "()");
        log.info(":::::: ==> Request Batch  : " + batch);
        log.info(":::::: ==> Request Params : " + JSON.toJSONString(requestParams));

        Object responseInfo = joinPoint.proceed();

        long end = System.currentTimeMillis();

        log.info(":::::: <== " + className + "." + method.getName() + "()");
        log.info(":::::: <== Request  Batch : " + batch);
        log.info(":::::: <== Time Consuming : " + (end - start) + " ms");
        log.info(":::::: <== Response  Info : " + JSON.toJSONString(responseInfo));

        return responseInfo;
    }
}

/**
 * 用于获取方法参数名所使用到的对象
 *
 * @author : leims
 * @date : 2019-08-20 14:49
 */
class SingleLocalVariableTableParameterNameDiscoverer {
    private static volatile LocalVariableTableParameterNameDiscoverer instance = null;

    private SingleLocalVariableTableParameterNameDiscoverer() {
    }

    public static synchronized LocalVariableTableParameterNameDiscoverer getInstance() {
        if (instance == null) {
            instance = new LocalVariableTableParameterNameDiscoverer();
        }
        return instance;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值