Feign自定义日志

自定Spring Cloud Feign日志输出
参考DefaultFeignLoggerFactory类实现自己的日志工厂实现类。

场景说明:将原有的debug级别,修改成info级别

第一步:实现FeignLoggerFactory工厂接口,InfoFeignLoggerFactory 是FeignConfig静态内部类

public static class InfoFeignLoggerFactory implements FeignLoggerFactory {
 
    @Override
    public Logger create(Class type) {
        return new InfoFeignLogger(LoggerFactory.getLogger(type));
    }
}

第二步: 继承feign.Logger实现info级别日志输出,InfoFeignLogger使用slf4j日志工具,此处只是简单的实现了info级别日志输出,如果想效率更高请参考Slf4jLogger添加一些日记级别判断

public class InfoFeignLogger extends feign.Logger {
 
    private final Logger logger;
 
    public InfoFeignLogger(Logger logger) {
        this.logger = logger;
    }
 
    @Override
    protected void log(String configKey, String format, Object... args) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format(methodTag(configKey) + format, args));
        }
    }
}

第三步:日志工厂InfoFeignLoggerFactory注册到spring 容器中,使用spring java Config

@Configuration
public class FeignConfig {
 
    @Bean
    Logger.Level feignLevel() {
        return Logger.Level.FULL;
    }
 
    @Bean
    FeignLoggerFactory infoFeignLoggerFactory() {
        return new InfoFeignLoggerFactory();
    }
 
}

这样就实现了自定义的日志打印了,简单吧,让我们看看效果,是不是变成INFO日志级别了。运行结果如下:

2019-05-22 17:53:31.548  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] ---> POST http://localhost:8888/user HTTP/1.1
2019-05-22 17:53:31.549  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] Content-Type: application/json;charset=UTF-8
2019-05-22 17:53:31.549  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] Content-Length: 27
2019-05-22 17:53:31.549  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] 
2019-05-22 17:53:31.549  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] {"id":null,"name":"张三"}
2019-05-22 17:53:31.549  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] ---> END HTTP (27-byte body)
2019-05-22 17:53:31.621  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] <--- HTTP/1.1 200 (71ms)
2019-05-22 17:53:31.621  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] content-length: 0
2019-05-22 17:53:31.621  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] date: Wed, 22 May 2019 09:53:31 GMT
2019-05-22 17:53:31.621  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] 
2019-05-22 17:53:31.621  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#save] <--- END HTTP (0-byte body)
2019-05-22 17:53:31.623  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] ---> GET http://localhost:8888/user/1 HTTP/1.1
2019-05-22 17:53:31.624  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] ---> END HTTP (0-byte body)
2019-05-22 17:53:31.649  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] <--- HTTP/1.1 200 (24ms)
2019-05-22 17:53:31.654  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] content-type: application/json;charset=UTF-8
2019-05-22 17:53:31.660  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] date: Wed, 22 May 2019 09:53:31 GMT
2019-05-22 17:53:31.661  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] transfer-encoding: chunked
2019-05-22 17:53:31.661  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] 
2019-05-22 17:53:31.663  INFO 17489 --- [           main] com.zxw.feign.UserFeign                  : [UserFeign#getUserByID] {"id":"1","name":"张三"}

细心的同学可能会发现,为什么我们的FeignLoggerFactory就起作用了,其实这是spring 强大的条件装配功能

当FeignLoggerFactory不存在时才加载默认DefaultFeignLoggerFactory,如果对spring 条件装配感兴趣的同学可以看下官网,spring boot对spring条件装配扩展了很多注解。

@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
   return new DefaultFeignLoggerFactory(logger);
}

总结

此章节介绍了自定义Feign 日志输出,通过实现FeignLoggerFactory工厂类接口和继承feign.Logger类,还使用了 slf4j日志工具,在项目开发过程中建议使用slf4j这样项目在更换日志框架也不用修改源代码了,扩展性更强。

你可能感兴趣的:(Spring,Cloud,Hoxton)