Skip to content

Commit

Permalink
Message Title :
Browse files Browse the repository at this point in the history
- logging filter change
Detail :
   ADD :
      - add unique identifier request and response info
   UPDATE :
      - wrapping request and response ContentCaching
   REMOVE : X
   USELESS ACT : X
  • Loading branch information
godchiken committed Jun 28, 2019
1 parent 704d994 commit c9102f8
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/main/java/com/kbh/elk/app/config/filter/LoggingFilter.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,15 +9,19 @@
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;
import javax.servlet.http.HttpServletRequest;
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
Expand All @@ -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<String, Object> requestMap = LogUtil.makeLoggingRequestMap(request);
chain.doFilter(request, response);
Map<String, Object> 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())));
}
}

0 comments on commit c9102f8

Please sign in to comment.