Skip to content

Commit

Permalink
Update: 에러로깅 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Jun 5, 2024
1 parent dda369e commit 32561b2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/gt/genti/error/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/gt/genti/error/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
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
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ExpectedException.class)
protected ResponseEntity<ApiResult<ExpectedException>> handleExpectedException(final ExpectedException exception) {
protected ResponseEntity<ApiResult<ExpectedException>> 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);
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/gt/genti/other/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 32561b2

Please sign in to comment.