全局記錄Feign的請求和響應日志方式
項目裡使用瞭Feign進行遠程調用,有時為瞭問題排查,需要開啟請求和響應日志
下面簡介一下如何開啟Feign日志:
註:本文基於
- spring-boot-starter-parent 2.3.4.RELEASE
- spring-cloud-starter-openfeign 2.2.3.RELEASE
1、項目裡定義FeignClient接口
package com.example.demo.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "deom", url = "https://www.baidu.com") public interface FeignDemo { @GetMapping("/") String test(); }
2、單個FeignClient接口開啟日志
在 application.yml 裡指定Feign接口日志級別為DEBUG,類型為FULL:
註:com.example.demo.feign.FeignDemo就是上面定義的FeignClient接口
logging: level: com.example.demo.feign.FeignDemo: debug # 下面的配置,也可以寫代碼代替 # @Bean # public Logger.Level level() { return Logger.Level.FULL; } feign: client: config: default: loggerLevel: full
OK瞭,重啟項目,調用 FeignDemo.test() 方法後,會輸出如下日志:
2020-10-13 11:46:24.161 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] —> GET https://www.baidu.com HTTP/1.1
2020-10-13 11:46:24.162 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] —> END HTTP (0-byte body)
2020-10-13 11:46:24.255 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] <— HTTP/1.1 200 OK (93ms)
2020-10-13 11:46:24.255 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] content-length: 2443
2020-10-13 11:46:24.255 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] content-type: text/html
2020-10-13 11:46:24.256 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] date: Tue, 13 Oct 2020 03:46:24 GMT
2020-10-13 11:46:24.256 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] server: bfe
2020-10-13 11:46:24.256 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test]
2020-10-13 11:46:24.257 DEBUG 20824 — [nio-8080-exec-4] com.example.demo.feign.FeignDemo : [FeignDemo#test] <!DOCTYPE html>
<!–STATUS OK–><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
3、所有FeignClient接口 開啟日志
上面的方法,隻能開啟單個FeignClient接口,如果項目裡有10個接口,那麼要在yml裡配置10項,而且以後添加新的FeignClient,還要記得去修改yml配置,太麻煩。
所以,下面是開啟所有FeignClient接口日志的配置:
3.1、修改FeignConfiguration
自定義feign.Logger,如下:
package com.example.demo.cacheDemo; import feign.slf4j.Slf4jLogger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FeignConfiguration { @Bean public feign.Logger logger() { return new Slf4jLogger(); } }
3.2、還是修改 application.yml 配置
logging: level: # 刪除具體的FeignClient接口配置,隻保留這一個就好瞭 feign.Logger: debug # 也可以寫代碼代替 # @Bean # public Logger.Level level() { return Logger.Level.FULL; } feign: client: config: default: loggerLevel: full
3.3、OK瞭,此時項目裡
不管新增多少個 FeignClient,都會輸出日志。
4、重寫FeignClient輸出日志
根據上面輸出的日志,可以看到是多條INFO日志,在並發時,很有可能會互相幹擾,而且格式也無法調整。
我們知道,Feign默認情況下,是使用 feign.Client.Default 發起http請求;
我們可以重寫Client,並註入Bean來替換掉 feign.Client.Default,從而實現日志記錄,當然也可以做其它任意事情瞭,比如添加Header。下面是註入Bean的代碼:
// 默認不註入,如果yml配置裡有 logging.level.beinet.cn.demostudy.MyClient 才註入 @Bean @ConditionalOnProperty("logging.level.beinet.cn.demostudy.MyClient") MyClient getClient() throws NoSuchAlgorithmException, KeyManagementException { // 忽略SSL校驗 SSLContext ctx = SSLContext.getInstance("SSL"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); return new MyClient(ctx.getSocketFactory(), (hostname, sslSession) -> true); }
下面是重寫的Client完整代碼:
package beinet.cn.demostudy; import feign.Client; import feign.Request; import feign.Response; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StreamUtils; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; import java.io.*; import java.util.Collection; import java.util.Map; @Slf4j public class MyClient extends Client.Default { public MyClient(SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier) { super(socketFactory, hostnameVerifier); } @Override public Response execute(Request request, Request.Options options) throws IOException { StringBuilder sb = new StringBuilder("[log started]\r\n"); sb.append(request.httpMethod()).append(" ").append(request.url()).append("\r\n"); CombineHeaders(sb, request.headers()); // 請求Header CombineBody(sb, request.body()); long costTime = -1; Exception exception = null; BufferingFeignClientResponse response = null; long begin = System.currentTimeMillis(); try { response = new BufferingFeignClientResponse(super.execute(request, options)); costTime = (System.currentTimeMillis() - begin); } catch (Exception exp) { costTime = (System.currentTimeMillis() - begin); exception = exp; throw exp; } finally { sb.append("\r\nResponse cost time(ms): ").append(String.valueOf(costTime)); if (response != null) sb.append(" status: ").append(response.status()); sb.append("\r\n"); if (response != null) { CombineHeaders(sb, response.headers()); // 響應Header sb.append("Body:\r\n").append(response.body()).append("\r\n"); } if (exception != null) { sb.append("Exception:\r\n ").append(exception.getMessage()).append("\r\n"); } sb.append("\r\n[log ended]"); log.debug(sb.toString()); } Response ret = response.getResponse().toBuilder() .body(response.getBody(), response.getResponse().body().length()).build(); response.close(); return ret; } private static void CombineHeaders(StringBuilder sb, Map<String, Collection<String>> headers) { if (headers != null && !headers.isEmpty()) { sb.append("Headers:\r\n"); for (Map.Entry<String, Collection<String>> ob : headers.entrySet()) { for (String val : ob.getValue()) { sb.append(" ").append(ob.getKey()).append(": ").append(val).append("\r\n"); } } } } private static void CombineBody(StringBuilder sb, byte[] body) { if (body == null || body.length <= 0) return; sb.append("Body:\r\n").append(new String(body)).append("\r\n"); } static final class BufferingFeignClientResponse implements Closeable { private Response response; private byte[] body; private BufferingFeignClientResponse(Response response) { this.response = response; } private Response getResponse() { return this.response; } private int status() { return this.response.status(); } private Map<String, Collection<String>> headers() { return this.response.headers(); } private String body() throws IOException { StringBuilder sb = new StringBuilder(); try (InputStreamReader reader = new InputStreamReader(getBody())) { char[] tmp = new char[1024]; int len; while ((len = reader.read(tmp, 0, tmp.length)) != -1) { sb.append(new String(tmp, 0, len)); } } return sb.toString(); } private InputStream getBody() throws IOException { if (this.body == null) { this.body = StreamUtils.copyToByteArray(this.response.body().asInputStream()); } return new ByteArrayInputStream(this.body); } @Override public void close() { this.response.close(); } } }
輸出日志示例:
2020-10-15 16:48:26.081 DEBUG 15664 — [ main] beinet.cn.demostudy.MyClient : [log started]
POST https://www.baidu.com?flg=3
Headers:
Content-Length: 14
Content-Type: text/plain;charset=UTF-8
Body:
abcde我是dddResponse cost time(ms): 207 status: 200
Headers:
content-length: 2443
content-type: text/html
date: Thu, 15 Oct 2020 08:48:27 GMT
server: bfe
Body:
<!DOCTYPE html>百度的html</html>
5、使用Aspect切面輸出日志
這個不推薦,因為它無法打印出具體的url、header等數據,大傢可以參考:
不需要yml配置,直接在項目裡添加如下代碼即可:
package com.example.demo.feign; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect @Component @Slf4j public class FeignAspect { // 這個也行 @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)") // 參考 https://github.com/spring-cloud/spring-cloud-openfeign/issues/322 @Pointcut("@within(org.springframework.cloud.openfeign.FeignClient)") public void feignClientPointcut() { } @Around("feignClientPointcut()") public Object feignAround(ProceedingJoinPoint joinPoint) throws Throwable { return logAround(joinPoint); } private static ObjectMapper mapper = new ObjectMapper(); private Object logAround(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); Object result = null; Exception exception = null; try { result = point.proceed(); } catch (Exception exp) { exception = exp; } long time = System.currentTimeMillis() - beginTime; saveLog(point, result, exception, time); if (exception != null) { throw exception; } return result; } private static void saveLog(ProceedingJoinPoint joinPoint, Object result, Exception exception, long time) { Dto dto = new Dto(); dto.setCostTime(time); try { if (exception != null) { dto.setExp(exception.toString()); } if (result != null) { dto.setResult(serial(result)); } MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //請求的 類名、方法名 String className = joinPoint.getTarget().getClass().getName(); String signName = signature.getDeclaringTypeName(); if (!signName.equalsIgnoreCase(className)) signName += "|" + className; dto.setClas(signName); String methodName = signature.getName(); dto.setMethod(methodName); //請求的參數 Object[] args = joinPoint.getArgs(); if (args != null && args.length > 0) { dto.setPara(serial(args)); } } catch (Exception e) { dto.setExp(e.toString()); } if (exception != null) { log.warn(dto.toString()); } else { log.info(dto.toString()); } } private static String serial(Object obj) { try { return mapper.writeValueAsString(obj); } catch (Exception ex) { return obj.toString(); } } @Data private static class Dto { /** * 調用類名 */ private String clas; /** * 調用方法名 */ private String method; /** * 調用的參數 */ private String para; /** * 方法返回結果 */ private String result; /** * 執行時長,毫秒 */ private long costTime; /** * 備註 */ private String remark; /** * 出現的異常 */ private String exp; } }
OK,輸出的日志如下:
2020-10-13 14:24:48.321 INFO 21304 — [nio-8080-exec-3] com.example.demo.feign.FeignAspect : FeignAspect.Dto(clas=com.example.demo.feign.FeignDemo|com.sun.proxy.$Proxy72, method=test, para=null, result="<!DOCTYPE html>\r\n<!–STATUS OK–><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=百度一下 class=\"bg s_btn\" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ '\" name=\"tj_login\" class=\"lb\">登錄</a>');\r\n </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
註:Feign官方文檔:
https://cloud.spring.io/spring-cloud-openfeign/reference/html/
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- FeignClient服務器拋出異常客戶端處理方案
- Feign調用全局異常處理解決方案
- feign如何打印出http請求
- 基於spring cloud多個消費端重復定義feign client的問題
- feign 打印日志不顯示的問題及解決