import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.lang.reflect.Method;
import java.util.List;
@Component
@Aspect
@Slf4j
public class FeignLogAdvice {
@Pointcut("execution(* com.xxx.xxx.*Feign.*(..))")
public void logPointCut() {
}
@Around("logPointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
Signature sig = pjp.getSignature();
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
String url = method.getAnnotation(PostMapping.class) != null
? method.getAnnotation(PostMapping.class).value()[0]
: method.getAnnotation(GetMapping.class).value()[0];
log.info("feign请求结束,本次请求耗时:{}ms, url:{}, params----------:{}, result-----------:{} ",
System.currentTimeMillis() - start, url, printObj(pjp.getArgs(), url), printObj(result, url));
return result;
}
private final ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
private Object printObj(Object o, String url){
return maskFeignUrls.contains(url) ? Base64Utils.encode(objectMapper.writeValueAsBytes(o)) : o;
}
@Value("#{'${maskFeignUrls:}'.split(',')}")
private List<String> maskFeignUrls;
}