chore: ログを改善2

This commit is contained in:
usbharu 2024-09-13 02:43:44 +09:00
parent 94365e09f2
commit 831bb17fca
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 12 additions and 1 deletions

View File

@ -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<T : Any, R>(
protected val transaction: Transaction,
@ -26,6 +27,7 @@ abstract class AbstractApplicationService<T : Any, R>(
) : ApplicationService<T, R> {
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<R> {
internalExecute(command, principal)
@ -39,6 +41,8 @@ abstract class AbstractApplicationService<T : Any, R>(
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
logger.warn("Command execution error", e)
throw e
} finally {
MDC.remove("applicationService")
}
}

View File

@ -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)
}
}