From a55ca285852321b6bae3e1dce8fcf0a928d0b62c Mon Sep 17 00:00:00 2001 From: wwan13 Date: Sat, 17 Aug 2024 01:03:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore=20:=20=EC=9A=94=EC=B2=AD=20=EB=A1=9C?= =?UTF-8?q?=EA=B9=85=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC=EB=A6=AC=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grida-core/core-api/build.gradle.kts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/grida-core/core-api/build.gradle.kts b/grida-core/core-api/build.gradle.kts index 7bf7c1a4..80f5316b 100644 --- a/grida-core/core-api/build.gradle.kts +++ b/grida-core/core-api/build.gradle.kts @@ -16,6 +16,9 @@ dependencies { // security implementation("com.github.wwan13:winter-security:0.0.10") + // logging-request + implementation("com.github.wwan13:spring-request-logger:0.0.3") + // api docs testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc") testImplementation("com.github.wwan13.kotlin-dsl-rest-docs:impl-mockmvc:1.2.9") From 31bd4e1ca6587876e565658df4ab8721262b1314 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Sat, 17 Aug 2024 01:04:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor=20:=20request=20logger=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/org/grida/config/CoreApiConfig.kt | 23 ++++++- .../support/requestlogger/EnableLogRequest.kt | 8 --- .../grida/support/requestlogger/LogFilter.kt | 39 ------------ .../requestlogger/LogFilterRegistrar.kt | 18 ------ .../requestlogger/RequestLogContext.kt | 60 ------------------- 5 files changed, 20 insertions(+), 128 deletions(-) delete mode 100644 grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/EnableLogRequest.kt delete mode 100644 grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt delete mode 100644 grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt delete mode 100644 grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt diff --git a/grida-core/core-api/src/main/kotlin/org/grida/config/CoreApiConfig.kt b/grida-core/core-api/src/main/kotlin/org/grida/config/CoreApiConfig.kt index 35905ebf..65ad2fdd 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/config/CoreApiConfig.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/config/CoreApiConfig.kt @@ -1,10 +1,27 @@ package org.grida.config -import org.grida.support.requestlogger.EnableLogRequest +import io.wwan13.springrequestlogger.configure.EnableLoggingRequest +import io.wwan13.springrequestlogger.configure.LogMessageConfigurer +import io.wwan13.springrequestlogger.context.RequestContext import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.context.annotation.Configuration +import java.util.function.Function @Configuration @ConfigurationPropertiesScan -@EnableLogRequest -class CoreApiConfig +@EnableLoggingRequest +class CoreApiConfig : LogMessageConfigurer { + + override fun format(): Function { + return Function { context -> + """ + | + | ${context.method} '${context.uri}' - ${context.status} (${context.elapsed} s) + | >> Request Headers : ${context.requestHeaders} + | >> Request Params : ${context.requestParams} + | >> Request Body : ${context.requestBody} + | >> Response Body : ${context.responseBody} + """.trimMargin() + } + } +} diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/EnableLogRequest.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/EnableLogRequest.kt deleted file mode 100644 index 533d5d57..00000000 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/EnableLogRequest.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.grida.support.requestlogger - -import org.springframework.context.annotation.Import - -@Retention(AnnotationRetention.RUNTIME) -@Target(AnnotationTarget.CLASS) -@Import(LogFilterRegistrar::class) -annotation class EnableLogRequest() diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt deleted file mode 100644 index 1dd9fcf7..00000000 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt +++ /dev/null @@ -1,39 +0,0 @@ -package org.grida.support.requestlogger - -import com.fasterxml.jackson.databind.ObjectMapper -import mu.KotlinLogging -import org.springframework.web.filter.OncePerRequestFilter -import org.springframework.web.util.ContentCachingRequestWrapper -import org.springframework.web.util.ContentCachingResponseWrapper -import javax.servlet.FilterChain -import javax.servlet.http.HttpServletRequest -import javax.servlet.http.HttpServletResponse - -private val log = KotlinLogging.logger {} - -class LogFilter( - private val objectMapper: ObjectMapper -) : OncePerRequestFilter() { - - override fun doFilterInternal( - request: HttpServletRequest, - response: HttpServletResponse, - filterChain: FilterChain - ) { - val requestWrapper = ContentCachingRequestWrapper(request) - val responseWrapper = ContentCachingResponseWrapper(response) - - val requestOccurred = System.currentTimeMillis() - filterChain.doFilter(requestWrapper, responseWrapper) - val requestCompleted = System.currentTimeMillis() - val requestElapsed = (requestCompleted - requestOccurred) / 1000.0 - - val logContext = RequestLogContext.of(requestWrapper, responseWrapper, requestElapsed) - log.info(logContext.toLogMessage(objectMapper)) - responseWrapper.copyBodyToResponse() - } - - override fun shouldNotFilter(request: HttpServletRequest): Boolean { - return request.requestURI.startsWith("/api/actuator") - } -} diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt deleted file mode 100644 index 049f27b0..00000000 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt +++ /dev/null @@ -1,18 +0,0 @@ -package org.grida.support.requestlogger - -import com.fasterxml.jackson.databind.ObjectMapper -import org.springframework.boot.web.servlet.FilterRegistrationBean -import org.springframework.context.annotation.Bean - -class LogFilterRegistrar { - - @Bean - fun logFilter( - objectMapper: ObjectMapper - ): FilterRegistrationBean { - val filterRegistration = FilterRegistrationBean() - filterRegistration.filter = LogFilter(objectMapper) - filterRegistration.order = 0 - return filterRegistration - } -} diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt deleted file mode 100644 index a0a683ab..00000000 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt +++ /dev/null @@ -1,60 +0,0 @@ -package org.grida.support.requestlogger - -import com.fasterxml.jackson.databind.ObjectMapper -import org.springframework.http.HttpStatus -import org.springframework.web.util.ContentCachingRequestWrapper -import org.springframework.web.util.ContentCachingResponseWrapper -import java.util.Enumeration - -data class RequestLogContext( - val method: String, - val uri: String, - val status: HttpStatus, - val elapsed: Double, - val requestHeaders: Map, - val requestParams: Map, - val requestBody: String, - val responseBody: String -) { - - fun toLogMessage(objectMapper: ObjectMapper): String { - return """ - | - |$method $uri - $status ($elapsed s) - |>> REQUEST HEADERS : $requestHeaders - |>> REQUEST PARAMS : $requestParams - |>> REQUEST BODY : ${objectMapper.readTree(requestBody.ifBlank { "{}" })} - |>> RESPONSE BODY : ${objectMapper.readTree(responseBody)} - """.trimMargin() - } - - companion object { - fun of( - request: ContentCachingRequestWrapper, - response: ContentCachingResponseWrapper, - elapsed: Double - ): RequestLogContext { - return RequestLogContext( - method = request.method, - uri = request.requestURI, - status = HttpStatus.valueOf(response.status), - elapsed = elapsed, - requestHeaders = extractAsMap(request, request.headerNames), - requestParams = extractAsMap(request, request.parameterNames), - requestBody = String(request.contentAsByteArray), - responseBody = String(response.contentAsByteArray), - ) - } - - private fun extractAsMap( - request: ContentCachingRequestWrapper, - names: Enumeration - ): Map { - val result = mutableMapOf() - names.asIterator().forEach { - result.put(it, request.getHeader(it)) - } - return result - } - } -}