From c0890d3472a1d401975a72ac44ca3dfb53f06da3 Mon Sep 17 00:00:00 2001 From: kkanggu Date: Fri, 18 Aug 2023 22:38:53 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20Exception=20=EB=B0=9C=EC=83=9D=20?= =?UTF-8?q?=EC=8B=9C=20=EC=83=81=EC=84=B8=ED=95=9C=20Logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/RequestLoggingFilter.kt | 72 ++++++++++--------- .../common/exception/ExceptionHandler.kt | 32 ++++++++- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/com/example/daitssuapi/common/configuration/RequestLoggingFilter.kt b/src/main/kotlin/com/example/daitssuapi/common/configuration/RequestLoggingFilter.kt index 777f1632..87ab4f75 100644 --- a/src/main/kotlin/com/example/daitssuapi/common/configuration/RequestLoggingFilter.kt +++ b/src/main/kotlin/com/example/daitssuapi/common/configuration/RequestLoggingFilter.kt @@ -24,31 +24,36 @@ class RequestLoggingFilter : OncePerRequestFilter() { return } + request.setAttribute("isLogged", false) + val wrappedRequest = ContentCachingRequestWrapper(request) val wrappedResponse = ContentCachingResponseWrapper(response) filterChain.doFilter(wrappedRequest, wrappedResponse) - val requestInfo = makeRequestInfo(wrappedRequest) - val responseInfo = makeResponseInfo(wrappedResponse) + if (!(request.getAttribute("isLogged") as Boolean)) { + val requestInfo = makeRequestInfo(wrappedRequest) + val responseInfo = makeResponseInfo(wrappedResponse) - log.info { - """ - { - "request" : $requestInfo, - "response" : $responseInfo + log.info { + """ + { + "request" : $requestInfo, + "response" : $responseInfo + } + """.trimIndent() } - """.trimIndent() } } - fun makeRequestInfo(request: ContentCachingRequestWrapper): String { - val requestHeaders = request.headerNames.toList().associateWith { - request.getHeader(it) - } - val requestBody = getBody(request.contentAsByteArray) + companion object { + fun makeRequestInfo(request: ContentCachingRequestWrapper): String { + val requestHeaders = request.headerNames.toList().associateWith { + request.getHeader(it) + } + val requestBody = getBody(request.contentAsByteArray) - return """ + return """ { "client ip" : "${request.remoteAddr}", "uri" : "${request.requestURI}", @@ -56,36 +61,37 @@ class RequestLoggingFilter : OncePerRequestFilter() { "body" : $requestBody } """.trimIndent() - } - - fun makeResponseInfo(response: ContentCachingResponseWrapper): String { - val responseHeaders = response.headerNames.toList().associateWith { - response.getHeader(it) } - val responseBody = getBody(response.contentAsByteArray) - response.copyBodyToResponse() - return """ + fun makeResponseInfo(response: ContentCachingResponseWrapper): String { + val responseHeaders = response.headerNames.toList().associateWith { + response.getHeader(it) + } + val responseBody = getBody(response.contentAsByteArray) + response.copyBodyToResponse() + + return """ { "status" : "${response.status}", "headers" : ${jacksonObjectMapper().writeValueAsString(responseHeaders)}, "body" : $responseBody } """.trimIndent() - } - - private fun getBody(body: ByteArray): String { - if (body.isEmpty()) { - return "{}" } - val readTree = jacksonObjectMapper().readTree(body) - val filteredJson: ObjectNode = jacksonObjectMapper().createObjectNode() + private fun getBody(body: ByteArray): String { + if (body.isEmpty()) { + return "{}" + } - readTree.fields().forEach { (key, value) -> - filteredJson.set(key, value) - } + val readTree = jacksonObjectMapper().readTree(body) + val filteredJson: ObjectNode = jacksonObjectMapper().createObjectNode() - return jacksonObjectMapper().writeValueAsString(filteredJson) + readTree.fields().forEach { (key, value) -> + filteredJson.set(key, value) + } + + return jacksonObjectMapper().writeValueAsString(filteredJson) + } } } diff --git a/src/main/kotlin/com/example/daitssuapi/common/exception/ExceptionHandler.kt b/src/main/kotlin/com/example/daitssuapi/common/exception/ExceptionHandler.kt index 8ea147d3..2bd0995b 100644 --- a/src/main/kotlin/com/example/daitssuapi/common/exception/ExceptionHandler.kt +++ b/src/main/kotlin/com/example/daitssuapi/common/exception/ExceptionHandler.kt @@ -1,19 +1,24 @@ package com.example.daitssuapi.common.exception +import com.example.daitssuapi.common.configuration.RequestLoggingFilter import com.example.daitssuapi.common.dto.Response +import jakarta.servlet.http.HttpServletRequest import mu.KLogger import mu.KotlinLogging import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice +import org.springframework.web.util.ContentCachingRequestWrapper @RestControllerAdvice class ExceptionHandler() { private val log: KLogger = KotlinLogging.logger {} @ExceptionHandler(Exception::class) - fun handleException(exception: Exception): ResponseEntity> { + fun handleException(exception: Exception, request: HttpServletRequest): ResponseEntity> { + val requestInfo = RequestLoggingFilter.makeRequestInfo(ContentCachingRequestWrapper(request)) + lateinit var response: Response var httpStatus: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR @@ -21,15 +26,36 @@ class ExceptionHandler() { is BaseException -> { response = Response(code = exception.errorCode.code, message = exception.errorCode.message, data = null) httpStatus = exception.httpStatus - log.warn("${exception.errorCode} + $httpStatus") + + log.warn { + """ + { + "request" : $requestInfo, + "code" : "${exception.errorCode}", + "message" : "${exception.errorCode.message}", + "status" : "$httpStatus" + } + """.trimIndent() + } } else -> { response = Response(code = 1, message = "", data = null) - log.error("${exception.message}") + + log.warn { + """ + { + "request" : $requestInfo, + "message" : "${exception.message?.replace("\"", "'")}", + "status" : "$httpStatus" + } + """.trimIndent() + } } } + request.setAttribute("isLogged", true) + return ResponseEntity.status(httpStatus).body(response) } }