SpringBoot記錄Http請求日志的方法
在使用Spring Boot開發(fā) web api 的時候希望把 request,request header ,response reponse header , uri, method 等等的信息記錄到我們的日志中,方便我們排查問題,也能對系統(tǒng)的數(shù)據(jù)做一些統(tǒng)計。
Spring 使用了 DispatcherServlet 來攔截并分發(fā)請求,我們只要自己實現(xiàn)一個 DispatcherServlet 并在其中對請求和響應做處理打印到日志中即可。
我們實現(xiàn)一個自己的分發(fā) Servlet ,它繼承于 DispatcherServlet,我們實現(xiàn)自己的 doDispatch(HttpServletRequest request, HttpServletResponse response) 方法。
public class LoggableDispatcherServlet extends DispatcherServlet { private static final Logger logger = LoggerFactory.getLogger("HttpLogger"); private static final ObjectMapper mapper = new ObjectMapper(); @Override protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request); ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); //創(chuàng)建一個 json 對象,用來存放 http 日志信息 ObjectNode rootNode = mapper.createObjectNode(); rootNode.put("uri", requestWrapper.getRequestURI()); rootNode.put("clientIp", requestWrapper.getRemoteAddr()); rootNode.set("requestHeaders", mapper.valueToTree(getRequestHeaders(requestWrapper))); String method = requestWrapper.getMethod(); rootNode.put("method", method); try { super.doDispatch(requestWrapper, responseWrapper); } finally { if(method.equals("GET")) { rootNode.set("request", mapper.valueToTree(requestWrapper.getParameterMap())); } else { JsonNode newNode = mapper.readTree(requestWrapper.getContentAsByteArray()); rootNode.set("request", newNode); } rootNode.put("status", responseWrapper.getStatus()); JsonNode newNode = mapper.readTree(responseWrapper.getContentAsByteArray()); rootNode.set("response", newNode); responseWrapper.copyBodyToResponse(); rootNode.set("responseHeaders", mapper.valueToTree(getResponsetHeaders(responseWrapper))); logger.info(rootNode.toString()); } } private Map<String, Object> getRequestHeaders(HttpServletRequest request) { Map<String, Object> headers = new HashMap<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); headers.put(headerName, request.getHeader(headerName)); } return headers; } private Map<String, Object> getResponsetHeaders(ContentCachingResponseWrapper response) { Map<String, Object> headers = new HashMap<>(); Collection<String> headerNames = response.getHeaderNames(); for (String headerName : headerNames) { headers.put(headerName, response.getHeader(headerName)); } return headers; }
在 LoggableDispatcherServlet 中,我們可以通過 HttpServletRequest 中的 InputStream 或 reader 來獲取請求的數(shù)據(jù),但如果我們直接在這里讀取了流或內(nèi)容,到后面的邏輯將無法進行下去,所以需要實現(xiàn)一個可以緩存的 HttpServletRequest。好在 Spring 提供這樣的類,就是 ContentCachingRequestWrapper 和 ContentCachingResponseWrapper, 根據(jù)官方的文檔這兩個類正好是來干這個事情的,我們只要將 HttpServletRequest 和 HttpServletResponse 轉(zhuǎn)化即可。
HttpServletRequest wrapper that caches all content read from the input stream and reader, and allows this content to be retrieved via a byte array.
Used e.g. by AbstractRequestLoggingFilter. Note: As of Spring Framework 5.0, this wrapper is built on the Servlet 3.1 API.HttpServletResponse wrapper that caches all content written to the output stream and writer, and allows this content to be retrieved via a byte array.
Used e.g. by ShallowEtagHeaderFilter. Note: As of Spring Framework 5.0, this wrapper is built on the Servlet 3.1 API.
實現(xiàn)好我們的 LoggableDispatcherServlet后,接下來就是要指定使用 LoggableDispatcherServlet 來分發(fā)請求。
@SpringBootApplication public class SbDemoApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(SbDemoApplication.class, args); } @Bean public ServletRegistrationBean dispatcherRegistration() { return new ServletRegistrationBean(dispatcherServlet()); } @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet() { return new LoggableDispatcherServlet(); } }
增加一個簡單的 Controller 來測試一下
@RestController @RequestMapping("/hello") public class HelloController { @RequestMapping(value = "/word", method = RequestMethod.POST) public Object hello(@RequestBody Object object) { return object; } }
使用 curl 發(fā)送一個 Post 請求:
$ curl --header "Content-Type: application/json" \ --request POST \ --data '{"username":"xyz","password":"xyz"}' \ http://localhost:8080/hello/word {"username":"xyz","password":"xyz"}
查看打印的日志:
{ "uri":"/hello/word", "clientIp":"0:0:0:0:0:0:0:1", "requestHeaders":{ "content-length":"35", "host":"localhost:8080", "content-type":"application/json", "user-agent":"curl/7.54.0", "accept":"*/*" }, "method":"POST", "request":{ "username":"xyz", "password":"xyz" }, "status":200, "response":{ "username":"xyz", "password":"xyz" }, "responseHeaders":{ "Content-Length":"35", "Date":"Sun, 17 Mar 2019 08:56:50 GMT", "Content-Type":"application/json;charset=UTF-8" } }
當然打印出來是在一行中的,我進行了一下格式化。我們還可以在日志中增加請求的時間,耗費的時間以及異常信息等。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java線程中斷?interrupt?和?LockSupport解析
這篇文章主要為大家介紹了java線程中斷?interrupt?和?LockSupport示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02spring無法引入注解及import org.springframework.web.bind.annota
本文主要介紹了spring無法引入注解及import org.springframework.web.bind.annotation.*報錯的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06Java網(wǎng)絡(luò)編程之IO模型阻塞與非阻塞簡要分析
這篇文章主要介紹Java網(wǎng)絡(luò)編程中的IO模型阻塞與非阻塞簡要分析,文中附有示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Spring Cloud Netflix架構(gòu)淺析(小結(jié))
這篇文章主要介紹了Spring Cloud Netflix架構(gòu)淺析(小結(jié)),詳解的介紹了Spring Cloud Netflix的概念和組件等,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01java結(jié)合WebSphere MQ實現(xiàn)接收隊列文件功能
WebSphereMQ,也稱MQSeries,以一致的、可靠的和易于管理的方式來連接應用程序,并為跨部門、企業(yè)范圍的集成提供了可靠的基礎(chǔ)。通過為重要的消息和事務(wù)提供可靠的、一次且僅一次的傳遞,MQ可以處理復雜的通信協(xié)議,并動態(tài)地將消息傳遞工作負載分配給可用的資源。2015-10-10iOS獲取AppIcon and LaunchImage''s name(app圖標和啟動圖片名字)
這篇文章主要介紹了iOS獲取AppIcon and LaunchImage's name(app圖標和啟動圖片名字)的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧2016-08-08利用Postman和Chrome的開發(fā)者功能探究項目(畢業(yè)設(shè)計項目)
這篇文章主要介紹了利用Postman和Chrome的開發(fā)者功能探究項目(畢業(yè)設(shè)計項目),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12