From 32561b21a88977fda8925e8b4b64377c3a3fbceb Mon Sep 17 00:00:00 2001 From: BYEONGRYEOL Date: Wed, 5 Jun 2024 22:16:43 +0900 Subject: [PATCH] =?UTF-8?q?Update:=20=EC=97=90=EB=9F=AC=EB=A1=9C=EA=B9=85?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gt/genti/error/ExpectedException.java | 8 ++++++++ .../genti/error/GlobalExceptionHandler.java | 6 ++++-- .../com/gt/genti/other/config/WebConfig.java | 20 +++++++++++++++++++ .../other/interceptor/LoggingInterceptor.java | 19 ++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/gt/genti/other/config/WebConfig.java create mode 100644 src/main/java/com/gt/genti/other/interceptor/LoggingInterceptor.java diff --git a/src/main/java/com/gt/genti/error/ExpectedException.java b/src/main/java/com/gt/genti/error/ExpectedException.java index 9b7e0b51..efa55a91 100644 --- a/src/main/java/com/gt/genti/error/ExpectedException.java +++ b/src/main/java/com/gt/genti/error/ExpectedException.java @@ -10,4 +10,12 @@ public ExpectedException(ErrorCode errorCode, Object... args) { super(errorCode.getMessage(args)); this.errorCode = errorCode; } + + public String toString() { + return """ + HttpStatusCode : [%d] + ExceptionCode : [%s] + Message : [%s] + """.formatted(errorCode.getHttpStatusCode().value(), errorCode.getCode(), this.getMessage()); + } } diff --git a/src/main/java/com/gt/genti/error/GlobalExceptionHandler.java b/src/main/java/com/gt/genti/error/GlobalExceptionHandler.java index bfff9e22..6ab5db6f 100644 --- a/src/main/java/com/gt/genti/error/GlobalExceptionHandler.java +++ b/src/main/java/com/gt/genti/error/GlobalExceptionHandler.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.support.WebExchangeBindException; import org.springframework.web.method.annotation.HandlerMethodValidationException; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -21,9 +22,10 @@ public class GlobalExceptionHandler { @ExceptionHandler(ExpectedException.class) - protected ResponseEntity> handleExpectedException(final ExpectedException exception) { + protected ResponseEntity> handleExpectedException(final HttpServletRequest request, + final ExpectedException exception) { log.error(""" - 예외 발생, 예외 내용 : \n \n %s""".formatted(exception.toString())); + [Error] uri : %s \n%s""".formatted(request.getRequestURI(), exception.toString())); return error(exception); } diff --git a/src/main/java/com/gt/genti/other/config/WebConfig.java b/src/main/java/com/gt/genti/other/config/WebConfig.java new file mode 100644 index 00000000..6db9db17 --- /dev/null +++ b/src/main/java/com/gt/genti/other/config/WebConfig.java @@ -0,0 +1,20 @@ +package com.gt.genti.other.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import com.gt.genti.other.interceptor.LoggingInterceptor; + +import lombok.RequiredArgsConstructor; + +@Configuration +@RequiredArgsConstructor +public class WebConfig implements WebMvcConfigurer { + private final LoggingInterceptor loggingInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(loggingInterceptor); + } +} diff --git a/src/main/java/com/gt/genti/other/interceptor/LoggingInterceptor.java b/src/main/java/com/gt/genti/other/interceptor/LoggingInterceptor.java new file mode 100644 index 00000000..32ac8c99 --- /dev/null +++ b/src/main/java/com/gt/genti/other/interceptor/LoggingInterceptor.java @@ -0,0 +1,19 @@ +package com.gt.genti.other.interceptor; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@Component +public class LoggingInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String requestUrl = request.getRequestURL().toString(); + String method = request.getMethod(); + System.out.println(String.format("request: %s %s", method, requestUrl)); + return true; + } +}