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 dev.usbharu.hideout.core.domain.model.support.principal.Principal
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.MDC
abstract class AbstractApplicationService<T : Any, R>( abstract class AbstractApplicationService<T : Any, R>(
protected val transaction: Transaction, protected val transaction: Transaction,
@ -26,6 +27,7 @@ abstract class AbstractApplicationService<T : Any, R>(
) : ApplicationService<T, R> { ) : ApplicationService<T, R> {
override suspend fun execute(command: T, principal: Principal): R { override suspend fun execute(command: T, principal: Principal): R {
return try { return try {
MDC.put("applicationService", this::class.simpleName)
logger.debug("START {}", command::class.simpleName) logger.debug("START {}", command::class.simpleName)
val response = transaction.transaction<R> { val response = transaction.transaction<R> {
internalExecute(command, principal) internalExecute(command, principal)
@ -39,6 +41,8 @@ abstract class AbstractApplicationService<T : Any, R>(
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) { } catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
logger.warn("Command execution error", e) logger.warn("Command execution error", e)
throw 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 dev.usbharu.hideout.core.infrastructure.springframework.oauth2.HideoutUserDetails
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse import jakarta.servlet.http.HttpServletResponse
import org.slf4j.LoggerFactory
import org.slf4j.MDC import org.slf4j.MDC
import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.jwt.Jwt import org.springframework.security.oauth2.jwt.Jwt
@ -14,7 +15,7 @@ import java.util.*
class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor {
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
MDC.put(requestId, UUID.randomUUID().toString()) MDC.put(requestId, UUID.randomUUID().toString())
MDC.put(Companion.handler, handler.toString())
val userDetailId = when (val principal = SecurityContextHolder.getContext().authentication?.principal) { val userDetailId = when (val principal = SecurityContextHolder.getContext().authentication?.principal) {
is HideoutUserDetails -> { is HideoutUserDetails -> {
principal.userDetailsId principal.userDetailsId
@ -29,10 +30,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor {
} }
} }
if (userDetailId != null) { if (userDetailId != null) {
MDC.put(userId, userDetailId.toString()) MDC.put(userId, userDetailId.toString())
} }
logger.info("START")
return true return true
} }
@ -56,10 +60,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor {
private fun removeMdc() { private fun removeMdc() {
MDC.remove(requestId) MDC.remove(requestId)
MDC.remove(userId) MDC.remove(userId)
MDC.remove(handler)
} }
companion object { companion object {
const val requestId: String = "requestId" const val requestId: String = "requestId"
const val userId: String = "userId" const val userId: String = "userId"
const val handler: String = "handler"
private val logger = LoggerFactory.getLogger(ApplicationRequestLogInterceptor::class.java)
} }
} }