diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt new file mode 100644 index 00000000..44b54057 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt @@ -0,0 +1,33 @@ +package dev.usbharu.hideout.core.infrastructure.springframework + +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.slf4j.MDC +import org.springframework.stereotype.Component +import org.springframework.web.servlet.AsyncHandlerInterceptor +import java.util.* + +@Component +class MdcInterceptor() : AsyncHandlerInterceptor { + override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { + MDC.put("request", UUID.randomUUID().toString()) + return true + } + + override fun afterConcurrentHandlingStarted( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any + ) { + MDC.remove("request") + } + + override fun afterCompletion( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any, + ex: Exception? + ) { + MDC.remove("request") + } +} \ No newline at end of file diff --git a/hideout-core/src/main/resources/log4j2.xml b/hideout-core/src/main/resources/log4j2.xml index 2f0d6625..7b5e5c96 100644 --- a/hideout-core/src/main/resources/log4j2.xml +++ b/hideout-core/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - +