SpringBoot中如何打印Http請求日志
前言
在項(xiàng)目開發(fā)過程中經(jīng)常需要使用Http協(xié)議請求第三方接口,而所有針對第三方的請求都強(qiáng)烈推薦打印請求日志,以便問題追蹤。最常見的做法是封裝一個(gè)Http請求的工具類,在里面定制一些日志打印,但這種做法真的比較low,本文將分享一下在Spring Boot中如何優(yōu)雅的打印Http請求日志。
一、spring-rest-template-logger實(shí)戰(zhàn)
1、引入依賴
<dependency>
<groupId>org.hobsoft.spring</groupId>
<artifactId>spring-rest-template-logger</artifactId>
<version>2.0.0</version>
</dependency>
2、配置RestTemplate
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplateBuilder()
.customizers(new LoggingCustomizer())
.build();
return restTemplate;
}
}
3、配置RestTemplate請求的日志級別
logging.level.org.hobsoft.spring.resttemplatelogger.LoggingCustomizer = DEBUG
4、單元測試
@SpringBootTest
@Slf4j
class LimitApplicationTests {
@Resource
RestTemplate restTemplate;
@Test
void testHttpLog() throws Exception{
String requestUrl = "https://api.uomg.com/api/icp";
//構(gòu)建json請求參數(shù)
JSONObject params = new JSONObject();
params.put("domain", "www.baidu.com");
//請求頭聲明請求格式為json
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//創(chuàng)建請求實(shí)體
HttpEntity<String> entity = new HttpEntity<>(params.toString(), headers);
//執(zhí)行post請求,并響應(yīng)返回字符串
String resText = restTemplate.postForObject(requestUrl, entity, String.class);
System.out.println(resText);
}
}
5、日志打印
2023-06-25 10:43:44.780 DEBUG [Sleuth,b6ac76a169b1af33,b6ac76a169b1af33] 63501 --- [ main] o.h.s.r.LoggingCustomizer : Request: POST https://api.uomg.com/api/icp {"domain":"www.baidu.com"}
2023-06-25 10:43:45.849 DEBUG [Sleuth,b6ac76a169b1af33,b6ac76a169b1af33] 63501 --- [ main] o.h.s.r.LoggingCustomizer : Response: 200 {"code":200901,"msg":"查詢域名不能為空"}
{"code":200901,"msg":"查詢域名不能為空"}
可以看見,我們只是簡單的向RestTemplate中配置了LoggingCustomizer,就實(shí)現(xiàn)了http請求的日志通用化打印,在Request和Response中分別打印出了請求的入?yún)⒑晚憫?yīng)結(jié)果。
6、自定義日志打印格式
可以通過繼承LogFormatter接口實(shí)現(xiàn)自定義的日志打印格式。
RestTemplate restTemplate = new RestTemplateBuilder() .customizers(new LoggingCustomizer(LogFactory.getLog(LoggingCustomizer.class), new MyLogFormatter())) .build();
默認(rèn)的日志打印格式化類DefaultLogFormatter:
public class DefaultLogFormatter implements LogFormatter {
private static final Charset DEFAULT_CHARSET;
public DefaultLogFormatter() {
}
//格式化請求
public String formatRequest(HttpRequest request, byte[] body) {
String formattedBody = this.formatBody(body, this.getCharset(request));
return String.format("Request: %s %s %s", request.getMethod(), request.getURI(), formattedBody);
}
//格式化響應(yīng)
public String formatResponse(ClientHttpResponse response) throws IOException {
String formattedBody = this.formatBody(StreamUtils.copyToByteArray(response.getBody()), this.getCharset(response));
return String.format("Response: %s %s", response.getStatusCode().value(), formattedBody);
}
……
}
可以參考DefaultLogFormatter的實(shí)現(xiàn),定制自定的日志打印格式化類MyLogFormatter。
二、原理分析
首先分析下LoggingCustomizer類,通過查看源碼發(fā)現(xiàn),LoggingCustomizer繼承自RestTemplateCustomizer,RestTemplateCustomizer表示RestTemplate的定制器,RestTemplateCustomizer是一個(gè)函數(shù)接口,只有一個(gè)方法customize,用來擴(kuò)展定制RestTemplate的屬性。
@FunctionalInterface
public interface RestTemplateCustomizer {
void customize(RestTemplate restTemplate);
}
LoggingCustomizer的核心方法customize:
public class LoggingCustomizer implements RestTemplateCustomizer{
public void customize(RestTemplate restTemplate) {
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
//向restTemplate中注入了日志攔截器
restTemplate.getInterceptors().add(new LoggingInterceptor(this.log, this.formatter));
}
}
日志攔截器中的核心方法:
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
private final Log log;
private final LogFormatter formatter;
public LoggingInterceptor(Log log, LogFormatter formatter) {
this.log = log;
this.formatter = formatter;
}
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (this.log.isDebugEnabled()) {
//打印http請求的入?yún)?
this.log.debug(this.formatter.formatRequest(request, body));
}
ClientHttpResponse response = execution.execute(request, body);
if (this.log.isDebugEnabled()) {
//打印http請求的響應(yīng)結(jié)果
this.log.debug(this.formatter.formatResponse(response));
}
return response;
}
}
原理小結(jié):
通過向restTemplate對象中添加自定義的日志攔截器LoggingInterceptor,使用自定義的格式化類DefaultLogFormatter來打印http請求的日志。
三、優(yōu)化改進(jìn)
可以借鑒spring-rest-template-logger的實(shí)現(xiàn),通過Spring Boot Start自動(dòng)化配置原理, 聲明自動(dòng)化配置類RestTemplateAutoConfiguration,自動(dòng)配置RestTemplate。
這里只是提供一些思路,搞清楚相關(guān)組件的原理后,我們就可以輕松定制通用的日志打印組件。
@Configuration
public class RestTemplateAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplateBuilder()
.customizers(new LoggingCustomizer())
.build();
return restTemplate;
}
}
總結(jié)
這里的優(yōu)雅打印并不是針對http請求日志打印的格式,而是通過向RestTemplate中注入攔截器實(shí)現(xiàn)通用性強(qiáng)、代碼侵入性小、具有可定制性的日志打印方式。
在Spring Boot中http請求都推薦采用RestTemplate模板類,而無需自定義http請求的靜態(tài)工具類,比如HttpUtil- 推薦在配置類中采用@Bean將RestTemplate類配置成統(tǒng)一通用的單例對象注入到Spring 容器中,而不是每次使用的時(shí)候重新聲明。
- 推薦采用攔截器實(shí)現(xiàn)http請求日志的通用化打印
到此這篇關(guān)于SpringBoot中如何打印Http請求日志的文章就介紹到這了,更多相關(guān)SpringBoot打印Http請求日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解SpringBoot中控制器的動(dòng)態(tài)注冊與卸載
在項(xiàng)目開發(fā)中,通過動(dòng)態(tài)注冊和卸載控制器功能,可以根據(jù)業(yè)務(wù)場景和項(xiàng)目需要實(shí)現(xiàn)功能的動(dòng)態(tài)增加、刪除,提高系統(tǒng)的靈活性和可擴(kuò)展性,下面我們就來看看SpringBoot中控制器動(dòng)態(tài)注冊和卸載的詳細(xì)教程2025-07-07
SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫的實(shí)現(xiàn)
本文主要介紹了SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Mybatis逆向工程實(shí)現(xiàn)連接MySQL數(shù)據(jù)庫
本文主要介紹了Mybatis逆向工程實(shí)現(xiàn)連接MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java創(chuàng)建、讀取和更新Excel文檔的實(shí)現(xiàn)指南
還在用Java代碼逐行雕刻Excel文檔嗎,是時(shí)候升級你的生產(chǎn)力工具箱了,借助Spire.XLS for Java,無論是創(chuàng)建新報(bào)表、讀取關(guān)鍵數(shù)據(jù)還是實(shí)時(shí)更新內(nèi)容,現(xiàn)在你都能以簡單的代碼指令輕松實(shí)現(xiàn),所以本文給大家介紹了如何使用Java創(chuàng)建、讀取和更新Excel文檔,需要的朋友可以參考下2025-10-10
SpringBoot3和4的版本新特性及升級要點(diǎn)詳解
Spring Boot 4.0作為生態(tài)內(nèi)的重大更新,基于Spring Framework 6.1+ 構(gòu)建,帶來了一系列顛覆性優(yōu)化,這篇文章主要介紹了SpringBoot3和4的版本新特性及升級要點(diǎn)的相關(guān)資料,需要的朋友可以參考下2026-03-03
cmd中javac和java使用及注意事項(xiàng)詳解
這篇文章主要介紹了cmd中javac和java使用及注意事項(xiàng)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Java commons io包實(shí)現(xiàn)多線程同步圖片下載入門教程
這篇文章主要介紹了Java commons io包實(shí)現(xiàn)多線程同步圖片下載入門,commons io: 是針對開發(fā)IO流功能的工具類庫,其中包含了許多可調(diào)用的函數(shù),感興趣的朋友跟隨小編一起看看吧2021-04-04
java括號(hào)匹配算法求解(用棧實(shí)現(xiàn))
這篇文章主要介紹了java括號(hào)匹配算法求解(用棧實(shí)現(xiàn)),需要的朋友可以參考下2020-12-12
Java如何實(shí)現(xiàn)將類文件打包為jar包
這篇文章主要介紹了Java如何實(shí)現(xiàn)將類文件打包為jar包,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

