Feign 自定义日志配置

feign.Logger.Level
修改日志级别,包含4个不同级别,NONE,BASE,HEADERS,FULL

1. 基于配置文件yml

feign:
  client:
    config:
      default: #这里default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
        LoggerLevel: FULL
logging:
  level:
    com.yy: debug #com.yy为对应基础包名
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS

重启后查看日志,多了很多请求日志。
Feign 自定义日志配置
2. 基于java代码方式
声明一个Bean,FeginClientConfiguration

package com.yy.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;


public class FeginClientConfiguration {

    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }
}

修改启动类配置全局生效

package com.yy.order;


import com.yy.order.config.FeginClientConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@MapperScan("com.yy.order.mapper")
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeginClientConfiguration.class)
//全局有效
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }


    /**
     * 创建RestTemplate并注入Spring容器
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

修改客户端类,局部生效

package com.yy.order.clients;

import com.yy.order.config.FeginClientConfiguration;
import com.yy.order.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "userservice",configuration = FeginClientConfiguration.class)
//配置局部有效,只针对userservice服务
//@FeignClient("userservice")
// userservice是请求的服务名称
public interface UserClient {

    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

上一篇:蓝桥公费选拔 T214627 Cm4k3r返屋企


下一篇:vi 常用快捷键