Skip to content

Commit

Permalink
Merge pull request #110 from ItRecode/fix/BE-168
Browse files Browse the repository at this point in the history
[BE-168] HTTPLogFilter에서 body값 출력 안되던 문제
  • Loading branch information
Jaeyeop-Jung authored Jan 16, 2023
2 parents 2ce9aa7 + 0052081 commit 48dcf5c
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/main/java/com/recordit/server/logger/HTTPLogFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.springframework.stereotype.Component;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;
import org.springframework.web.util.WebUtils;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -39,10 +38,11 @@ public void doFilter(
);

long start = System.currentTimeMillis();
chain.doFilter(request, response);
chain.doFilter(requestWrapper, responseWrapper);
long end = System.currentTimeMillis();

if (isInExcludeURIForLog(request)) {
if (isInExcludeURIForLog(requestWrapper)) {
responseWrapper.copyBodyToResponse();
return;
}

Expand All @@ -56,35 +56,35 @@ public void doFilter(
((HttpServletRequest)request).getRequestURI(),
responseWrapper.getStatus(),
(end - start),
getHeaders((HttpServletRequest)request),
getHeaders(requestWrapper),
getRequestParameter(requestWrapper),
getRequestBody(requestWrapper),
getResponseBody(responseWrapper));
}

private boolean isInExcludeURIForLog(ServletRequest request) {
if (((HttpServletRequest)request).getRequestURI().contains("/api/swagger")) {
private boolean isInExcludeURIForLog(ContentCachingRequestWrapper requestWrapper) {
if ((requestWrapper.getRequestURI().contains("/api/swagger"))) {
return true;
}
if ("/v2/api-docs".equals(((HttpServletRequest)request).getRequestURI())) {
if ("/v2/api-docs".equals(requestWrapper.getRequestURI())) {
return true;
}
return false;
}

private Map getHeaders(HttpServletRequest request) {
private Map getHeaders(ContentCachingRequestWrapper requestWrapper) {
Map headerMap = new HashMap<>();

Enumeration headerArray = request.getHeaderNames();
Enumeration headerArray = requestWrapper.getHeaderNames();
while (headerArray.hasMoreElements()) {
String headerName = (String)headerArray.nextElement();
headerMap.put(headerName, request.getHeader(headerName));
headerMap.put(headerName, requestWrapper.getHeader(headerName));
}
return headerMap;
}

private String getRequestParameter(ContentCachingRequestWrapper request) {
Map<String, String[]> parameterMap = request.getParameterMap();
private String getRequestParameter(ContentCachingRequestWrapper requestWrapper) {
Map<String, String[]> parameterMap = requestWrapper.getParameterMap();
if (parameterMap.size() == 0) {
return "-";
}
Expand All @@ -101,13 +101,12 @@ private String getRequestParameter(ContentCachingRequestWrapper request) {
return sb.toString();
}

private String getRequestBody(ContentCachingRequestWrapper request) {
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
private String getRequestBody(ContentCachingRequestWrapper requestWrapper) {
if (requestWrapper != null) {
byte[] buf = requestWrapper.getContentAsByteArray();
if (buf.length > 0) {
try {
return new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
return new String(buf, 0, buf.length, requestWrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
return " - ";
}
Expand All @@ -116,18 +115,14 @@ private String getRequestBody(ContentCachingRequestWrapper request) {
return " - ";
}

private String getResponseBody(final HttpServletResponse response) throws IOException {
private String getResponseBody(ContentCachingResponseWrapper responseWrapper) throws IOException {
String payload = null;
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(
response,
ContentCachingResponseWrapper.class
);
if (wrapper != null) {
wrapper.setCharacterEncoding("UTF-8");
byte[] buf = wrapper.getContentAsByteArray();
if (responseWrapper != null) {
responseWrapper.setCharacterEncoding("UTF-8");
byte[] buf = responseWrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
wrapper.copyBodyToResponse();
payload = new String(buf, 0, buf.length, responseWrapper.getCharacterEncoding());
responseWrapper.copyBodyToResponse();
}
}
return null == payload ? " - " : payload;
Expand Down

0 comments on commit 48dcf5c

Please sign in to comment.