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