偽のクライアントの要求と応答 (応答時間を含む) をログに記録するための簡単な方法とカスタマイズされた方法があります。feign.Logger.Level Bean を注入する必要があります。それだけです。
- デフォルト/ストレート フォワード ウェイ
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.BASIC;
}
BASIC、FULL、HEADERS、NONE(デフォルト)のログレベルがあり 、詳細を確認できます
上記の Bean インジェクションにより、以下の形式で偽のリクエストとレスポンスのログが得られます。
リクエスト:
参照
log(configKey, "---> %s %s HTTP/1.1", request.httpMethod().name(), request.url());
ex:2019-09-26 12:50:12.163 [DEBUG] [http-nio-4200-exec-5] [com.sample.FeignClient:72] [FeignClient#getUser] ---> END HTTP (0-byte body)
ここで、configkey
手段はFeignClientClassName#FeignClientCallingMethodName
ex:ApiClient#apiMethod
です。
応答
参照
log(configKey, "<--- HTTP/1.1 %s%s (%sms)", status, reason, elapsedTime);
ex:2019-09-26 12:50:12.163 [DEBUG] [http-nio-4200-exec-5] [com.sample.FeignClient:72] [FeignClient#getUser] <--- HTTP/1.1 200 OK (341ms)
これelapsedTime
は、API 呼び出しにかかった応答時間です。
注: 偽のクライアント ロギングのデフォルトの方法を使用する場合は、基礎となるアプリケーションのロギング レベルも考慮する必要があります。feign.Slf4jLogger
これは、偽のリクエストと応答のクラス ロギングがDEBUG
レベルで詳細を示すためです (参照)。基礎となるロギング レベルが DEBUG を超える場合は、ロギング パッケージ/クラスに明示的なロガーを指定する必要がある場合がありますfeign
。そうしないと機能しません。
- カスタマイズ
された方法 カスタマイズした形式でログを記録したい場合は、
feign.Logger
クラスを拡張してログをカスタマイズできます。典型的な例として、リクエストとレスポンスのヘッダーの詳細をリストとして 1 行に記録したい場合 (デフォルトでは、Logger.Level.HEADERS はヘッダーを複数行に出力します):
package com.test.logging.feign;
import feign.Logger;
import feign.Request;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import static feign.Logger.Level.HEADERS;
@Slf4j
public class customFeignLogger extends Logger {
@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
if (logLevel.ordinal() >= HEADERS.ordinal()) {
super.logRequest(configKey, logLevel, request);
} else {
int bodyLength = 0;
if (request.requestBody().asBytes() != null) {
bodyLength = request.requestBody().asBytes().length;
}
log(configKey, "---> %s %s HTTP/1.1 (%s-byte body) %s", request.httpMethod().name(), request.url(), bodyLength, request.headers());
}
}
@Override
protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime)
throws IOException {
if (logLevel.ordinal() >= HEADERS.ordinal()) {
super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime);
} else {
int status = response.status();
Request request = response.request();
log(configKey, "<--- %s %s HTTP/1.1 %s (%sms) %s", request.httpMethod().name(), request.url(), status, elapsedTime, response.headers());
}
return response;
}
@Override
protected void log(String configKey, String format, Object... args) {
log.debug(format(configKey, format, args));
}
protected String format(String configKey, String format, Object... args) {
return String.format(methodTag(configKey) + format, args);
}
}
また、customFeignLogger クラス Bean を注入する必要があります。
@Bean
public customFeignLogger customFeignLogging() {
return new customFeignLogger();
}
自分で FeignClient を構築している場合は、カスタマイズされたロガーで構築できます。
Feign.builder().logger(new customFeignLogger()).logLevel(Level.BASIC).target(SomeFeignClient.class,"http://localhost:8080");