From c9102f838789d0931777c3d32f919d61ea55cbcf Mon Sep 17 00:00:00 2001 From: godchiken Date: Fri, 28 Jun 2019 17:18:20 +0900 Subject: [PATCH] Message Title : - logging filter change Detail : ADD : - add unique identifier request and response info UPDATE : - wrapping request and response ContentCaching REMOVE : X USELESS ACT : X --- .../elk/app/config/filter/LoggingFilter.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/kbh/elk/app/config/filter/LoggingFilter.java b/src/main/java/com/kbh/elk/app/config/filter/LoggingFilter.java index 054266f..4d2444d 100644 --- a/src/main/java/com/kbh/elk/app/config/filter/LoggingFilter.java +++ b/src/main/java/com/kbh/elk/app/config/filter/LoggingFilter.java @@ -1,5 +1,6 @@ package com.kbh.elk.app.config.filter; +import com.fasterxml.jackson.databind.ObjectMapper; import com.kbh.elk.app.util.LogUtil; import com.kbh.elk.app.util.RequestWrapper; import com.kbh.elk.app.util.ResponseWrapper; @@ -8,6 +9,8 @@ import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; import org.springframework.web.filter.OncePerRequestFilter; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; import javax.servlet.FilterChain; import javax.servlet.ServletException; @@ -15,8 +18,10 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; +import java.util.StringJoiner; +import java.util.UUID; -import static net.logstash.logback.argument.StructuredArguments.value; +import static java.lang.String.valueOf; @Slf4j @Component @@ -26,20 +31,33 @@ public class LoggingFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException { - HttpServletRequest request = new RequestWrapper(req); - HttpServletResponse response = new ResponseWrapper(res); + ContentCachingRequestWrapper request = new RequestWrapper(req); + ContentCachingResponseWrapper response = new ResponseWrapper(res); Map requestMap = LogUtil.makeLoggingRequestMap(request); chain.doFilter(request, response); Map responseMap = LogUtil.makeLoggingResponseMap(response); - log.info("{}", value("req", requestMap), value("res", responseMap)); + setUniqueIdentifier(requestMap,responseMap); + log.info(new ObjectMapper().writeValueAsString(requestMap)); + log.info(new ObjectMapper().writeValueAsString(responseMap)); MDC.clear(); - ((ResponseWrapper) response).copyBodyToResponse(); + response.copyBodyToResponse(); } @Override - protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { + protected boolean shouldNotFilter(HttpServletRequest request) { AntPathMatcher pathMatcher = new AntPathMatcher(); String targetPath = request.getServletPath(); - return pathMatcher.match("/favicon.ico",targetPath); + return pathMatcher.match("/favicon.ico", targetPath); + } + private void setUniqueIdentifier(Map requestMap, Map responseMap){ + String uniqueIdentifier = generateUniqueIdentifier(); + requestMap.put("UUID",uniqueIdentifier); + responseMap.put("UUID",uniqueIdentifier); + } + private String generateUniqueIdentifier() { + return valueOf(new StringJoiner("-") + .add("KBH") + .add("T") + .add(valueOf(UUID.randomUUID()))); } }