自定义feign日志打印spring组件

feign日志的打印看的实在太难受,所以自定义写了一个组件打印效果如下feign请求路径—> POST http://127.0.0.1:8080/testfeign请求参数—> {“type”:1}feign请求结果状态 —> 500 请求用时 —> 129msfeign回参 —> {“errorCode”:“502”,“message”:"",“success”:false}1、启动注解类```javaimport com.hzz.hzzframe.con
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值