728x90
반응형
로깅 내용
- UUID
- METHOD
- URL
1. LogInterceptor
backend/interceptor/LogInterceptor
package web.backend.interceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
@Slf4j
@RequiredArgsConstructor
@Component
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 uuid = UUID.randomUUID().toString();
String requestURI = request.getRequestURI();
String method = request.getMethod();
request.setAttribute(LOG_ID, uuid);
log.info("REQUEST [{}][{}][{}][{}]", uuid, method, 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 logId = (String) request.getAttribute(LOG_ID);
String requestURI = request.getRequestURI();
String method = request.getMethod();
log.info("RESPONSE [{}][{}][{}]", logId, method, requestURI);
if (ex != null) {
log.error("afterCompletion error!!", ex);
}
}
}
2. WebConfig
backend/WebConfig
package web.backend;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import web.backend.interceptor.LogInterceptor;
@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/**" //오류 페이지 경로
);
}
}
3. 테스트
728x90
반응형
'Back-End > Spring Boot' 카테고리의 다른 글
Sping Boot | Backend Project | 공통 응답 객체 ( Common Response ) (0) | 2022.09.28 |
---|---|
Sping Boot | Backend Project | BasicAspect, LogTraceAspect (0) | 2022.09.27 |
Sping Boot | Backend Project | CRUD with SpringDataJPA (0) | 2022.09.26 |
Sping Boot | Backend Project | Module (1) | 2022.09.26 |
Sping Boot | Backend Project | 환경 세팅하기 (1) | 2022.09.23 |