自定义注解-MethodReturnLog
package cn.swifthealth.his.config.aspect;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Target({TYPE,METHOD,FIELD})
@Retention(RUNTIME)
public @interface MethodReturnLog {
}
aspect方法切点-日志
package cn.swifthealth.his.config.aspect;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Slf4j
@Aspect
@Component
public class MethodReturnAspect {
@AfterReturning(value = "@annotation(returnLog)", returning = "result")
public void after(JoinPoint point, MethodReturnLog returnLog, Object result) {
String methodName = point.getSignature().getDeclaringType().getSimpleName() + "." + point.getSignature().getName();
log.info("execute finish-{}", methodName);
if (result != null) {
log.info("{}-result={}", methodName, JSONObject.toJSONString(result));
} else {
log.warn("{}-result is null!", methodName);
}
}
}