当前位置: 首页 > java >正文

feign日志学习记录

使用依赖

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.1.1.RELEASE</version></dependency>

1.通过@FeignClient查看feign配置

    org.springframework.cloud.openfeign.FeignClient/*** A custom <code>@Configuration</code> for the feign client. Can contain override* <code>@Bean</code> definition for the pieces that make up the client, for instance* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.** @see FeignClientsConfiguration for the defaults* @return list of configurations for feign client*/Class<?>[] configuration() default {};

如果没有配置默认使用FeignClientsConfiguration.

	@Autowired(required = false)private Logger logger;@Bean@ConditionalOnMissingBean(FeignLoggerFactory.class)public FeignLoggerFactory feignLoggerFactory() {return new DefaultFeignLoggerFactory(this.logger);}

使用DefaultFeignLoggerFactory创建了日志

	@Overridepublic Logger create(Class<?> type) {return this.logger != null ? this.logger : new Slf4jLogger(type);}

默认情况logger为空默认选用feign.slf4j.Slf4jLogger
只有logger是debug级别才会打印feign日志。

  public Slf4jLogger(Class<?> clazz) {this(LoggerFactory.getLogger(clazz));}@Overrideprotected void logRequest(String configKey, Level logLevel, Request request) {// 只有类日志级别为debug才会有feign日志输出,if (logger.isDebugEnabled()) {super.logRequest(configKey, logLevel, request);}}

org.slf4j.LoggerFactory会根据类名创建logger,

    public static Logger getLogger(Class<?> clazz) {Logger logger = getLogger(clazz.getName());if (DETECT_LOGGER_NAME_MISMATCH) {Class<?> autoComputedCallingClass = Util.getCallingClass();if (autoComputedCallingClass != null && nonMatchingClasses(clazz, autoComputedCallingClass)) {Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\".", logger.getName(),autoComputedCallingClass.getName()));Util.report("See " + LOGGER_NAME_MISMATCH_URL + " for an explanation");}}return logger;}

feign请求日志feign.Logger应请求日志为例,根据不同feign日志级别打印不同信息

  protected void logRequest(String configKey, Level logLevel, Request request) {log(configKey, "---> %s %s HTTP/1.1", request.httpMethod().name(), request.url());if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {for (String field : request.headers().keySet()) {for (String value : valuesOrEmpty(request.headers(), field)) {log(configKey, "%s: %s", field, value);}}int bodyLength = 0;if (request.body() != null) {bodyLength = request.body().length;if (logLevel.ordinal() >= Level.FULL.ordinal()) {String bodyText =request.charset() != null ? new String(request.body(), request.charset()) : null;log(configKey, ""); // CRLFlog(configKey, "%s", bodyText != null ? bodyText : "Binary data");}}log(configKey, "---> END HTTP (%s-byte body)", bodyLength);}}

feign日志级别

  public enum Level {/*** No logging.*/NONE,/*** Log only the request method and URL and the response status code and execution time.*/BASIC,/*** Log the basic information along with request and response headers.*/HEADERS,/*** Log the headers, body, and metadata for both requests and responses.*/FULL}

配置class类日志级别debug,后feign日志级别生效。

logging:level:com.example.feign.client: debug  # Feign客户端所在包的日志级别
spring:cloud:openfeign:client:config:default:  # 全局默认配置loggerLevel: FULLhello-feign-server:  # 指定服务配置loggerLevel: BASIC
http://www.xdnf.cn/news/17031.html

相关文章:

  • k8s+isulad 国产化技术栈云原生技术栈搭建1-VPC
  • VUE-第二季-01
  • python批量gif图片转jpg
  • 【DL学习笔记】深入学习tenser
  • Claude Code入门学习笔记(一)--Claude Code简介
  • ICCV 2025 | EPD-Solver:西湖大学发布并行加速扩散采样算法
  • 多线程异步日志系统与实现及 TCP/IP C/S 模型
  • 解剖 .NET 经典:从 Component 到 BackgroundWorker
  • AD方案(OpenLDAP或微软AD)适配信创存在的不足以及可能优化方案
  • Redis面试精讲 Day 9:Redis模块开发与扩展
  • 【数据迁移】Windows11 下将 Ubuntu 从 C 盘迁移到 D 盘
  • 每日面试题20:spring和spring boot的区别
  • Spring MVC 九大组件源码深度剖析(一):MultipartResolver - 文件上传的幕后指挥官
  • Go语言实战案例:TCP服务器与客户端通信
  • Uniapp一根数据线实现真机调试运行【uniapp如何把项目运行在手机上】
  • 昇思学习营-开发版-模型推理和性能优化
  • Kaggle 竞赛入门指南
  • Jetson Orin NX/NANO+ubuntu22.04+humble+MAVROS2安装教程
  • LeetCode - 合并两个有序链表 / 删除链表的倒数第 N 个结点
  • Nginx相关实验(2)
  • Linux服务器运维告警系统搭建完整指南
  • 使用AssemblyAI将音频数据转换成文本
  • Elastic 9.1/8.19:默认启用 BBQ,ES|QL 支持跨集群搜索(CCS)正式版,JOINS 正式版,集成 Azure AI Foundry
  • uni-app学习笔记01-项目初始化及相关文件
  • 控制建模matlab练习10:滞后补偿器
  • sqli-labs:Less-25关卡详细解析
  • Go语法:闭包
  • 【银行测试】银行票据项目业务+票据测试点分析(二)
  • Android 之 网络通信(HTTP/TCP/UDP/JSON)
  • LeetCode Hot 100,快速学习,不断更