当前位置:网站首页>Feign requests the log to be printed uniformly
Feign requests the log to be printed uniformly
2022-04-23 18:11:00 【dawnsun001】
@Slf4j
public class FeignLogger extends feign.Logger {
static ThreadLocal<Map<String, String>> logContext = new ThreadLocal();
static String PATH = "path";
static String METHOD = "method";
static String REQUEST_BODY = "body";
static String ELAPSED_TIME = " Time consuming ";
static String ELAPSED_TIME_UNIT = " millisecond ";
static String FEIGN_INVOKE_LOGGER = "feign Interface call ";
@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
Map<String, String> logMap = new HashMap<>(3);
logMap.put(PATH, request.url());
logMap.put(METHOD, request.method());
logMap.put(REQUEST_BODY, request.body() == null ? null :
request.charset() == null ? null : new String(request.body(), request.charset()));
logContext.set(logMap);
}
@Override
protected Response logAndRebufferResponse(
String configKey, Level logLevel, Response response, long elapsedTime) throws IOException {
Map<String, String> requetParam = logContext.get();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append(FEIGN_INVOKE_LOGGER).append(" ")
.append(requetParam.get(METHOD)).append(" ")
.append(response.status()).append(" ")
.append(requetParam.get(PATH)).append(" ")
.append(ELAPSED_TIME).append(elapsedTime).append(ELAPSED_TIME_UNIT);
if (requetParam.get(REQUEST_BODY) != null) {
stringBuilder.append(" Request entry :").append(requetParam.get(REQUEST_BODY));
}
logContext.remove();
// Returns the parameter
if (response.body() != null && !(response.status() == 204 || response.status() == 205)) {
byte[] bodyData = Util.toByteArray(response.body().asInputStream());
if (bodyData.length > 0) {
String responseBody = decodeOrDefault(bodyData, UTF_8, "Binary data");
stringBuilder
.append(" Return value :")
.append(responseBody.replaceAll("\\s*|\t|\r|\n", ""));
}
log.info(stringBuilder.toString());
return response.toBuilder().body(bodyData).build();
}
log.info(stringBuilder.toString());
return response;
}
protected IOException logIOException(String configKey, Level logLevel, IOException ioe, long elapsedTime) {
Map<String, String> requetParam = logContext.get();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append(FEIGN_INVOKE_LOGGER).append(" ")
.append(requetParam.get(METHOD)).append(" ")
.append(ioe.getClass().getSimpleName()).append(" ")
.append(requetParam.get(PATH)).append(" ")
.append(ELAPSED_TIME).append(elapsedTime).append(ELAPSED_TIME_UNIT);;
if (requetParam.get(REQUEST_BODY) != null) {
stringBuilder.append(" Request entry :").append(requetParam.get(REQUEST_BODY));
}
log.warn(stringBuilder.toString());
logContext.remove();
return ioe;
}
@Override
protected void log(String configKey, String format, Object... args) {
if (log.isInfoEnabled()) {
log.info(String.format(methodTag(configKey) + format, args));
}
}
}
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289018.html
边栏推荐
- Auto.js 自定义对话框
- Refcell in rust
- Install the yapiupload plug-in in idea and upload the API interface to the Yapi document
- C [file operation] read TXT text by line
- re正则表达式
- [UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
- Nodejs安装
- [UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
- Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
- MySQL auto start settings start with systemctl start mysqld
猜你喜欢
.105Location
C medium? This form of
Data stream encryption and decryption of C
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
From introduction to mastery of MATLAB (2)
idea中安装YapiUpload 插件将api接口上传到yapi文档上
Auto.js 自定义对话框
Gobang game based on pyGame Library
mysql自动启动设置用Systemctl start mysqld启动
MySQL auto start settings start with systemctl start mysqld
随机推荐
The vivado project corresponding to the board is generated by TCL script
深度学习经典网络解析目标检测篇(一):R-CNN
re正则表达式
_ FindText error
Classes and objects
Installation du docker redis
Crawl lottery data
Crawl the product data of cicada mother data platform
Implementation of image recognition code based on VGg convolutional neural network
Crawling mobile game website game details and comments (MQ + multithreading)
MATLAB小技巧(6)七种滤波方法比较
.105Location
Robocode tutorial 8 - advanced robot
Tensorflow tensor introduction
String function in MySQL
Implementation of k8s redis one master multi slave dynamic capacity expansion
Generate verification code
Box pointer of rust
C# 网络相关操作
Gobang game based on pyGame Library