当前位置:网站首页>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
边栏推荐
- C language to achieve 2048 small game direction merging logic
- Robocode tutorial 3 - Robo machine analysis
- Selenium + phantom JS crack sliding verification 2
- Crawl the product data of Xiaomi Youpin app
- 2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
- Process management command
- Robocode tutorial 8 - advanced robot
- Build openstack platform
- Gobang game based on pyGame Library
- C# 网络相关操作
猜你喜欢
Installation du docker redis
A few lines of code teach you to crawl lol skin pictures
Visualization of residential house prices
Batch export ArcGIS attribute table
.104History
Re regular expression
Data stream encryption and decryption of C
MySQL_ 01_ Simple data retrieval
Closure type of rust (difference between FN, fnmut and fnone)
深度学习经典网络解析目标检测篇(一):R-CNN
随机推荐
Crawling mobile game website game details and comments (MQ + multithreading)
.104History
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
Random number generation of C #
I / O multiplexing and its related details
Read excel, int digital time to time
解决允许在postman中写入注释请求接口方法
RC smart pointer in rust
How to ensure the security of futures accounts online?
Flash - Middleware
Docker 安裝 Redis
How to read literature
C#字节数组(byte[])和字符串相互转换
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
Arcpy adds fields and loop assignments to vector data
C [file operation] read TXT text by line
Map basemap Library
C byte array (byte []) and string are converted to each other
Robocode tutorial 8 - advanced robot
MATLAB从入门到精通(二)