Skip to content

Commit

Permalink
refactor: 에러가 아닌 상황에도 로깅하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Choi-JJunho committed Apr 11, 2024
1 parent f52c082 commit aae9bc1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package in.koreatech.koin.global.exception;

import java.time.format.DateTimeParseException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.context.support.DefaultMessageSourceResolvable;
Expand All @@ -17,8 +14,6 @@
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import in.koreatech.koin.global.auth.exception.AuthenticationException;
import in.koreatech.koin.global.auth.exception.AuthorizationException;
Expand All @@ -35,7 +30,6 @@ public ResponseEntity<Object> handleIllegalArgumentException(
IllegalArgumentException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage());
}

Expand All @@ -45,7 +39,6 @@ public ResponseEntity<Object> handleAuthorizationException(
AuthorizationException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.FORBIDDEN, e.getMessage());
}

Expand All @@ -55,7 +48,6 @@ public ResponseEntity<Object> handleAuthenticationException(
AuthenticationException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.UNAUTHORIZED, e.getMessage());
}

Expand All @@ -65,7 +57,6 @@ public ResponseEntity<Object> handleDataNotFoundException(
DataNotFoundException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.NOT_FOUND, e.getMessage());
}

Expand All @@ -75,7 +66,6 @@ public ResponseEntity<Object> handleDataNotFoundException(
DuplicationException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.CONFLICT, e.getMessage());
}

Expand All @@ -85,7 +75,6 @@ public ResponseEntity<Object> handleExternalServiceException(
ExternalServiceException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}

Expand All @@ -95,7 +84,6 @@ public ResponseEntity<Object> handleDateTimeParseException(
DateTimeParseException e
) {
log.warn(e.getMessage());
requestLogging(request);
return buildErrorResponse(HttpStatus.BAD_REQUEST, "잘못된 날짜 형식입니다. " + e.getParsedString());
}

Expand All @@ -119,7 +107,6 @@ public ResponseEntity<Object> handleException(
서버에서 에러가 발생했습니다. uri: {} {}
{}
""", request.getMethod(), request.getRequestURI(), detail);
requestLogging(request);
return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "서버에서 오류가 발생했습니다.");
}

Expand All @@ -144,7 +131,6 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
) {
HttpServletRequest request = ((ServletWebRequest) webRequest).getRequest();
log.warn("검증과정에서 문제가 발생했습니다. uri: {} {}, ", request.getMethod(), request.getRequestURI(), ex);
requestLogging(request);
String errorMessages = ex.getBindingResult().getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(", "));
Expand All @@ -158,47 +144,4 @@ private ResponseEntity<Object> buildErrorResponse(
var response = new ErrorResponse(httpStatus.value(), message);
return ResponseEntity.status(httpStatus).body(response);
}

private void requestLogging(HttpServletRequest request) {
log.info("request header: {}", getHeaders(request));
log.info("request query string: {}", getQueryString(request));
log.info("request body: {}", getRequestBody(request));
}

private Map<String, Object> getHeaders(HttpServletRequest request) {
Map<String, Object> headerMap = new HashMap<>();
Enumeration<String> headerArray = request.getHeaderNames();
while (headerArray.hasMoreElements()) {
String headerName = headerArray.nextElement();
headerMap.put(headerName, request.getHeader(headerName));
}
return headerMap;
}

private String getQueryString(HttpServletRequest httpRequest) {
String queryString = httpRequest.getQueryString();
if (queryString == null) {
return " - ";
}
return queryString;
}

private String getRequestBody(HttpServletRequest request) {
var wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper == null) {
return " - ";
}
try {
// body 가 읽히지 않고 예외처리 되는 경우에 캐시하기 위함
wrapper.getInputStream().readAllBytes();
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length == 0) {
return " - ";
}
return new String(buf, wrapper.getCharacterEncoding());
} catch (Exception e
) {
return " - ";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
Expand All @@ -15,6 +18,7 @@
import org.springframework.util.StopWatch;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
Expand Down Expand Up @@ -54,6 +58,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
MDC.put(REQUEST_ID, getRequestId(httpRequest));
stopWatch.start();
log.info("request start [uri: {} {}]", httpRequest.getMethod(), httpRequest.getRequestURI());
log.info("request header: {}", getHeaders(cachedRequest));
log.info("request query string: {}", getQueryString(cachedRequest));
log.info("request body: {}", getRequestBody(cachedRequest));
chain.doFilter(cachedRequest, response);
} finally {
stopWatch.stop();
Expand All @@ -76,4 +83,41 @@ private String getRequestId(HttpServletRequest httpRequest) {
}
return requestId;
}

private Map<String, Object> getHeaders(HttpServletRequest request) {
Map<String, Object> headerMap = new HashMap<>();
Enumeration<String> headerArray = request.getHeaderNames();
while (headerArray.hasMoreElements()) {
String headerName = headerArray.nextElement();
headerMap.put(headerName, request.getHeader(headerName));
}
return headerMap;
}

private String getQueryString(HttpServletRequest httpRequest) {
String queryString = httpRequest.getQueryString();
if (queryString == null) {
return " - ";
}
return queryString;
}

private String getRequestBody(HttpServletRequest request) {
var wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper == null) {
return " - ";
}
try {
// body 가 읽히지 않고 예외처리 되는 경우에 캐시하기 위함
wrapper.getInputStream().readAllBytes();
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length == 0) {
return " - ";
}
return new String(buf, wrapper.getCharacterEncoding());
} catch (Exception e
) {
return " - ";
}
}
}

0 comments on commit aae9bc1

Please sign in to comment.