在spring cloud feign调用微服务时,打印输出请求响应日志,可以快速定位问题,以下是详细步聚
1. 增加config
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLevel() {
return Logger.Level.FULL;
}
@Bean
Logger feignLogger(){
return new FeignLogger();
}
}
2. 重写feign.Logger
public class FeignLogger extends feign.Logger {
private final org.slf4j.Logger logger;
public FeignLogger() {
this(feign.Logger.class);
}
public FeignLogger(Class<?> clazz) {
this(LoggerFactory.getLogger(clazz));
}
public FeignLogger(String name) {
this(LoggerFactory.getLogger(name));
}
FeignLogger(org.slf4j.Logger logger) {
this.logger = logger;
}
@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
if (logger.isInfoEnabled()) {
super.logRequest(configKey, logLevel, request);
}
}
@Override
protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime)
throws IOException {
if (logger.isInfoEnabled()) {
return super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime);
}
return response;
}
@Override
protected void log(String configKey, String format, Object... args) {
if (logger.isInfoEnabled()) {
logger.info(String.format(methodTag(configKey) + format, args));
}
}
}
3. feignClient使用注解配置config类
@FeignClient(path = "/", name="user-service-login",url = "${user-service}", configuration = {FeignConfig.class})
public interface LoginApi {
/**
* 用户登录
* @param loginVo
* @return
* @throws Exception
*/
@PostMapping(path = "/login",consumes = "application/json")
@ResponseBody
ResultEntity<Object> login(@RequestBody LoginVo loginVo) throws Exception;
/**
* 用户注册
* @param registerVO
* @return
* @throws Exception
*/
@PostMapping(path = "/regist",consumes = "application/json")
@ResponseBody
ResultEntity<Object> regist(@RequestBody RegisterVO registerVO) throws Exception;
}
4. yaml配置
#配置feignClient类所在的包名
logging:
level:
com.xxx.web.admin.feignapi.client:info
输出效果
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] ---> POST http://127.0.0.1:8083/user-service/login HTTP/1.1】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] Content-Type: application/json】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] Content-Length: 83】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] 】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] {"userName":"NjcyMDkyNjA5QHFNvbQ==","password":"RlbjExMQ==","remmberMe":false}】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] ---> END HTTP (83-byte body)】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] <--- HTTP/1.1 200 (25ms)】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] content-type: application/json;charset=utf-8】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] date: Sat, 13 Jun 2020 07:59:03 GMT】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] transfer-encoding: chunked】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] 】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] {"success":true,"code":200,"msg":"SUCCESS","details":null,"ex":null,"data":{"registTime":1591933549000,"contractPhone":"13433335555","country":1754,"isLogout":0,"emailAddress":"678882609@qq.com","password":"d7861c1594ddd737a663edd75d029f24","createTime":1591933549000,"nickName":"cl","companyName":"maclstudio","id":2006121145491216,"userName":"tomy","token":"b8e5d3a9-f6ff-44d1-bcf2-8de1526ab3sdddd"}}】 feign.Logger(58) [http-nio-8080-exec-1] []
v_1.4.0 INFO 2020-06-13 15:59:03 【[LoginApi#login] <--- END HTTP (397-byte body)】 feign.Logger(58) [http-nio-8080-exec-1] []