-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a7d0dc
commit f36defe
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |