feign日志的打印看的实在太难受,所以自定义写了一个组件
打印效果如下
feign请求路径—> POST http://127.0.0.1:8080/test
feign请求参数—> {“type”:1}
feign请求结果状态 —> 500 请求用时 —> 129ms
feign回参 —> {“errorCode”:“502”,“message”:"",“success”:false}
1、启动注解类
```java
import com.hzz.hzzframe.config.feignlog.FeignLogConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author :hzz
* @description:TODO
* @date :2021/9/29 10:34
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({FeignLogConfig.class})
public @interface EnableFeignLog {
/**
* 不记录feign日志的类
* @return
*/
Class<?>[] withExceptionOfClass() default{};
/**
* 是否展示头部
* @return
*/
boolean whetherToShowHead() default false;
/**
* 是否打印内部调用日志
* @return
*/
boolean whetherToifInternalLogPrint() default false;
}
2、核心日志启动配置类
```java
import com.hzz.hzzframe.config.feignlog.annotation.EnableFeignLog;
import feign.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.openfeign.FeignLoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import java.util.Map;
/**
* @author :hzz
* @description:TODO
* @date :2021/9/29 9:13
*/
@Configuration
public class FeignLogConfig {
@Autowired
private ApplicationContext context;
@Bean
Logger.Level feignLevel() {
return Logger.Level.BASIC;
}
@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
Map<String, Object> enableFeiClass = context.getBeansWithAnnotation(EnableFeignLog.class);
Class<?> enableFeiCla = enableFeiClass.values().toArray()[0].getClass();
EnableFeignLog enableFeignLogAnnotation = AnnotationUtils.findAnnotation(enableFeiCla, EnableFeignLog.class);
return new MyFeignLoggerFactory(enableFeignLogAnnotation);
}
}
3、自定义日志打印类
import com.hzz.hzzframe.config.feignlog.annotation.EnableFeignLog;
import feign.Logger;
import feign.Request;
import feign.Response;
import feign.Util;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static feign.Util.*;
/**
* @author :hzz
* @description:TODO
* @date :2021/9/29 10:09
*/
public class MyFeignLogger extends Logger {
private final org.slf4j.Logger logger;
/**
* 自定义注解
*/
private EnableFeignLog enableFeignLogAnnotation;
public MyFeignLogger(EnableFeignLog enableFeignLogAnnotation) {
this(Logger.class, enableFeignLogAnnotation);
}
public MyFeignLogger(Class<?> clazz, EnableFeignLog enableFeignLogAnnotation) {
this(LoggerFactory.getLogger(clazz), enableFeignLogAnnotation);
}
MyFeignLogger