From 831bb17fcac93d8a0267b59e6422e35b85e53a96 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 13 Sep 2024 02:43:44 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=E3=83=AD=E3=82=B0=E3=82=92=E6=94=B9?= =?UTF-8?q?=E5=96=842?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/shared/AbstractApplicationService.kt | 4 ++++ .../springframework/ApplicationRequestLogInterceptor.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) 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