mirror of https://github.com/usbharu/Hideout.git
chore: ログを改善2
This commit is contained in:
parent
94365e09f2
commit
831bb17fca
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue