diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt index 9006f5fc..14877a92 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt @@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.application.shared import dev.usbharu.hideout.core.domain.model.support.principal.Principal import kotlinx.coroutines.CancellationException import org.slf4j.Logger +import org.slf4j.MDC abstract class AbstractApplicationService( protected val transaction: Transaction, @@ -26,6 +27,7 @@ abstract class AbstractApplicationService( ) : ApplicationService { override suspend fun execute(command: T, principal: Principal): R { return try { + MDC.put("applicationService", this::class.simpleName) logger.debug("START {}", command::class.simpleName) val response = transaction.transaction { internalExecute(command, principal) @@ -39,6 +41,8 @@ abstract class AbstractApplicationService( } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { logger.warn("Command execution error", e) throw e + } finally { + MDC.remove("applicationService") } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt index c368f482..c6986456 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt @@ -3,6 +3,7 @@ package dev.usbharu.hideout.core.infrastructure.springframework import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.HideoutUserDetails import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import org.slf4j.LoggerFactory import org.slf4j.MDC import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.oauth2.jwt.Jwt @@ -14,7 +15,7 @@ import java.util.* class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { MDC.put(requestId, UUID.randomUUID().toString()) - + MDC.put(Companion.handler, handler.toString()) val userDetailId = when (val principal = SecurityContextHolder.getContext().authentication?.principal) { is HideoutUserDetails -> { principal.userDetailsId @@ -29,10 +30,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { } } + + if (userDetailId != null) { MDC.put(userId, userDetailId.toString()) } + logger.info("START") return true } @@ -56,10 +60,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { private fun removeMdc() { MDC.remove(requestId) MDC.remove(userId) + MDC.remove(handler) } companion object { const val requestId: String = "requestId" const val userId: String = "userId" + const val handler: String = "handler" + private val logger = LoggerFactory.getLogger(ApplicationRequestLogInterceptor::class.java) } } \ No newline at end of file