import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiLog {
String desc() default "";
boolean timeSpan() default true;
}
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.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Aspect
@Slf4j(topic = "ApiLogNote")
public class MyExecuteLog {
@Around(value = "@within(apiLog)", argNames = "point,apiLog")
public Object processMethod(ProceedingJoinPoint point, ApiLog apiLog) throws Throwable {
return process(point, apiLog);
}
@Around(value = "@annotation(apiLog)", argNames = "point,apiLog")
public Object around(ProceedingJoinPoint point, ApiLog apiLog) throws Throwable {
return process(point, apiLog);
}
public static Object process(ProceedingJoinPoint pjp, ApiLog apiLog) throws Throwable {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
log.info("----------------------method[{}]start--------------------", method.getName());
log.info("方法描述:{}", apiLog.desc());
log.info("参数 :{}", pjp.getArgs());
long startTime = System.currentTimeMillis();
Object proceed = pjp.proceed();
long endTime = System.currentTimeMillis();
log.info("耗时:{}ss", endTime - startTime);
log.info("----------------------method[{}] end--------------------\n", method.getName());
return proceed;
}
参考:AOP处理类上或者方法上面的自定义或指定注解