diff --git a/build.gradle.kts b/build.gradle.kts index 5b8d220..d83a637 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,6 +40,7 @@ repositories { } dependencies { + implementation("org.springframework.boot:spring-boot-starter-aop") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-webflux") implementation("org.springframework.boot:spring-boot-starter-data-jdbc") diff --git a/src/main/kotlin/com/wafflestudio/spring2025/common/aop/LoggingAspect.kt b/src/main/kotlin/com/wafflestudio/spring2025/common/aop/LoggingAspect.kt new file mode 100644 index 0000000..4072b85 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/spring2025/common/aop/LoggingAspect.kt @@ -0,0 +1,25 @@ +package com.wafflestudio.spring2025.common.aop + +import com.wafflestudio.spring2025.common.exception.DomainException +import org.aspectj.lang.annotation.AfterThrowing +import org.aspectj.lang.annotation.Aspect +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +@Aspect +@Component +class LoggingAspect { + private val log = LoggerFactory.getLogger(javaClass) + + @AfterThrowing( + pointcut = "within(com.wafflestudio.spring2025.domain..service..*)", + throwing = "ex", + ) + fun logException(ex: Throwable) { + if (ex is DomainException && ex.httpErrorCode.is4xxClientError) { + log.warn("Expected domain exception: {}", ex.toString()) + } else { + log.error("Unhandled exception in service", ex) + } + } +} diff --git a/src/main/kotlin/com/wafflestudio/spring2025/common/filter/RequestIdFilter.kt b/src/main/kotlin/com/wafflestudio/spring2025/common/filter/RequestIdFilter.kt new file mode 100644 index 0000000..1e93fbc --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/spring2025/common/filter/RequestIdFilter.kt @@ -0,0 +1,30 @@ +package com.wafflestudio.spring2025.common.filter + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.slf4j.MDC +import org.springframework.core.Ordered +import org.springframework.core.annotation.Order +import org.springframework.stereotype.Component +import org.springframework.web.filter.OncePerRequestFilter +import java.util.UUID + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +class RequestIdFilter : OncePerRequestFilter() { + override fun doFilterInternal( + request: HttpServletRequest, + response: HttpServletResponse, + filterChain: FilterChain, + ) { + val requestId = UUID.randomUUID().toString() + MDC.put("requestId", requestId) + response.setHeader("X-Request-Id", requestId) + try { + filterChain.doFilter(request, response) + } finally { + MDC.remove("requestId") + } + } +} diff --git a/src/main/kotlin/com/wafflestudio/spring2025/domain/auth/AuthenticationInterceptor.kt b/src/main/kotlin/com/wafflestudio/spring2025/domain/auth/AuthenticationInterceptor.kt index 460dd58..a0d4a9f 100644 --- a/src/main/kotlin/com/wafflestudio/spring2025/domain/auth/AuthenticationInterceptor.kt +++ b/src/main/kotlin/com/wafflestudio/spring2025/domain/auth/AuthenticationInterceptor.kt @@ -17,11 +17,7 @@ class AuthenticationInterceptor : HandlerInterceptor { response: HttpServletResponse, handler: Any, ): Boolean { - logger.info("AuthenticationInterceptor pre-handle") - val handlerMethod = handler as? HandlerMethod ?: return true - logger.info(handlerMethod.toString()) - val authRequired = handlerMethod.hasMethodAnnotation(AuthRequired::class.java) || handlerMethod.beanType.isAnnotationPresent(AuthRequired::class.java) diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index ee7ef52..9e8e955 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -132,3 +132,14 @@ oci: vault: secret-ids: ocid1.vaultsecret.oc1.ap-chuncheon-1.amaaaaaat2m5lbqameflpkt7abivvrauqwheybzrkqipf7msv3ol6ikhd3aq region: ap-chuncheon-1 +logging: + level: + org: + springframework: + data: + jdbc: INFO + jdbc: + core: + JdbcTemplate: INFO + namedparam: + NamedParameterJdbcTemplate: INFO diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..972dfa8 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,30 @@ + + + + + + + ${PATTERN} + + + + + + + 0 + + + + + + + + + + + + + + +