Skip to content

Commit

Permalink
Feat: 진입점 정보 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
haeyonghahn committed Mar 8, 2024
1 parent 5a7d0dc commit f36defe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.dnd.gooding.springconfig.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class MdcLoggingInterceptor implements HandlerInterceptor {

public static final String REQUEST_CONTROLLER_MDC_KEY = "handler";

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (handler instanceof HandlerMethod handlerMethod) {
String handlerName = handlerMethod.getBeanType().getSimpleName();
String methodName = handlerMethod.getMethod().getName();
String controllerInfo = handlerName + "." + methodName;
MDC.put(REQUEST_CONTROLLER_MDC_KEY, controllerInfo);
}
return true;
}

/** MDC에 저장된 정보를 모두 초기화 한다. */
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
MDC.clear();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dnd.gooding.springconfig.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MdcLoggingInterceptor());
}
}

0 comments on commit f36defe

Please sign in to comment.