exception : handle exception(interceptor)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package hello.exception;
|
||||
|
||||
import hello.exception.filter.LogFilter;
|
||||
import hello.exception.interceptor.LogInterceptor;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
@@ -12,6 +14,14 @@ import javax.servlet.Filter;
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new LogInterceptor())
|
||||
.order(1)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/css/**", "*.ico", "/error", "/error-page/**"); // 오류 페이지 경로 추가
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<Filter> logFilter() {
|
||||
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package hello.exception.interceptor;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.UUID;
|
||||
@Slf4j
|
||||
public class LogInterceptor implements HandlerInterceptor {
|
||||
public static final String LOG_ID = "logId";
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String requestURI = request.getRequestURI();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
request.setAttribute(LOG_ID, uuid);
|
||||
log.info("REQUEST [{}][{}][{}][{}]", uuid, request.getDispatcherType(), requestURI, handler);
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
log.info("postHandle [{}]", modelAndView);
|
||||
}
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
String requestURI = request.getRequestURI();
|
||||
String logId = (String)request.getAttribute(LOG_ID);
|
||||
log.info("RESPONSE [{}][{}][{}]", logId, request.getDispatcherType(), requestURI);
|
||||
if (ex != null) {
|
||||
log.error("afterCompletion error!!", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user