自定义标签通过AOP实现日志增强


package com.taimi.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Description:
 * Author: taimi 37310
 * Version: 1.0
 * Create Date Time: 2022/2/14 23:11.
 * Update Date Time:
 *
 * @see
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ksdLog {
    String value() default "";
}
package com.taimi.aop;

import com.taimi.first.LogService;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description:
 * Author: taimi 37310
 * Version: 1.0
 * Create Date Time: 2022/2/14 23:25.
 * Update Date Time:
 *
 * @see
 */
@Component
@Aspect
@Slf4j
public class LogAspect {
    @Autowired
    private LogService logService;

    //定一个切入点
    @Pointcut("@annotation(com.taimi.aop.ksdLog)")
    public void logpointcut(){

    }


    //定义通知

    @Around("logpointcut()")
    public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            long startTime = System.currentTimeMillis();
            //执行方法

            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            String name = signature.getMethod().getName();
            String[] parameterNames = signature.getParameterNames();
            
            proceedingJoinPoint.proceed();
            String name1 = proceedingJoinPoint.getTarget().getClass().getName();

            long endTime = System.currentTimeMillis();

            long t = endTime-startTime;
            log.info("方法名:{}当前执行的时间是{}ms",name,t);
            logService.saveLog(name1
                    ,name,
                    t+"",
                    parameterNames.toString()
            );
        }catch (Throwable e){
            log.info("发生异常{}"+e);
            throw  e;

        }
    }

}

【KSD - CONSOLE】 2022-02-15 00:19:22:038 [main] [INFO ] com.taimi.first.UserService saveUser 26 - 用户注册…
【KSD - CONSOLE】 2022-02-15 00:19:22:038 [main] [INFO ] com.taimi.aop.LogAspect aroundAdvice 57 - 方法名:saveUser当前执行的时间是7ms
【KSD - CONSOLE】 2022-02-15 00:19:22:040 [main] [INFO ] com.taimi.first.LogService saveLog 32 - 你保存的日志:Logs(id=1, classname=com.taimi.first.UserService, method=saveUser, time=7, params=[Ljava.lang.String;@6f2e1024)
【KSD - CONSOLE】 2022-02-15 00:19:22:041 [main] [INFO ] com.taimi.first.UserService updateUser 34 - 用户更新…
【KSD - CONSOLE】 2022-02-15 00:19:22:041 [main] [INFO ] com.taimi.first.UserService delUser 39 - 用户删除…1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值