From f475db09352cf7611d7d597500f8512be3ec5d62 Mon Sep 17 00:00:00 2001 From: aesperer Date: Fri, 20 Oct 2023 10:26:01 +0900 Subject: [PATCH] create :: http logging aspect --- .../team/msg/common/aop/HttpLoggingAspect.kt | 88 +++++++++++++++++++ .../msg/common/{ => init}/DataInitializer.kt | 2 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 bitgouel-api/src/main/kotlin/team/msg/common/aop/HttpLoggingAspect.kt rename bitgouel-api/src/main/kotlin/team/msg/common/{ => init}/DataInitializer.kt (99%) diff --git a/bitgouel-api/src/main/kotlin/team/msg/common/aop/HttpLoggingAspect.kt b/bitgouel-api/src/main/kotlin/team/msg/common/aop/HttpLoggingAspect.kt new file mode 100644 index 000000000..284263918 --- /dev/null +++ b/bitgouel-api/src/main/kotlin/team/msg/common/aop/HttpLoggingAspect.kt @@ -0,0 +1,88 @@ +package team.msg.common.aop + +import org.aspectj.lang.JoinPoint +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation.Around +import org.aspectj.lang.annotation.Aspect +import org.aspectj.lang.annotation.Pointcut +import org.aspectj.lang.reflect.CodeSignature +import org.aspectj.lang.reflect.MethodSignature +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Component +import org.springframework.web.context.request.RequestContextHolder +import org.springframework.web.context.request.ServletRequestAttributes +import team.msg.common.logger.LoggerDelegator +import java.util.* +import kotlin.collections.HashMap +import kotlin.collections.HashSet + +@Aspect +@Component +class HttpLoggingAspect { + + private val log by LoggerDelegator() + + @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") + fun onRequest() {} + + @Around("onRequest()") + @Throws(Throwable::class) + fun logging(proceedingJoinPoint: ProceedingJoinPoint): Any? { + val request = (RequestContextHolder.currentRequestAttributes() as ServletRequestAttributes).request + val ip = request.remoteAddr + val method = request.method + val uri = request.requestURI + val sessionId = request.requestedSessionId + val params = request.queryString + val contentType = request.contentType + val userAgent = request.getHeader("User-Agent") + val signature: MethodSignature = proceedingJoinPoint.signature as MethodSignature + val className = signature.declaringType.simpleName + val methodName = signature.name + val headerNames = request.headerNames + val headerSet: MutableSet = HashSet() + + while (headerNames.hasMoreElements()) { + val headerName = headerNames.nextElement() + headerSet.add(headerName) + } + val code = UUID.randomUUID() + log.info( + "At {}#{} [Request:{}] IP: {}, Session-ID: {}, URI: {}, Params: {}, Content-Type: {}, User-Agent: {}, Headers: {}, Parameters: {}, Code: {}", + className, methodName, method, ip, sessionId, uri, params, contentType, userAgent, headerSet, params(proceedingJoinPoint), code + ) + val result = proceedingJoinPoint.proceed() + when (result) { + is ResponseEntity<*> -> { + log.info( + "At {}#{} [Response:{}] IP: {}, Session-ID: {}, Headers: {}, Response: {}, Status-Code: {}, Code: {}", + className, methodName, method, ip,sessionId, result.headers, result.body, result.statusCode, code + ) + } + + null -> { + log.info( + "At {}#{} [Response: null] IP: {}, Session-ID: {}, Code: {}", + className, methodName, ip, sessionId, code + ) + } + + else -> { + throw RuntimeException("유효하지 않은 Controller 반환 타입입니다.") + } + } + return result + } + + private fun params(joinPoint: JoinPoint): Map<*,*>? { + val codeSignature = joinPoint.signature as CodeSignature + val parameterNames = codeSignature.parameterNames + val args = joinPoint.args + val params: MutableMap = HashMap() + + for (i in parameterNames.indices) { + params[parameterNames[i]] = args[i] + } + return params + } +} \ No newline at end of file diff --git a/bitgouel-api/src/main/kotlin/team/msg/common/DataInitializer.kt b/bitgouel-api/src/main/kotlin/team/msg/common/init/DataInitializer.kt similarity index 99% rename from bitgouel-api/src/main/kotlin/team/msg/common/DataInitializer.kt rename to bitgouel-api/src/main/kotlin/team/msg/common/init/DataInitializer.kt index 9b084b6c3..a006f3e1f 100644 --- a/bitgouel-api/src/main/kotlin/team/msg/common/DataInitializer.kt +++ b/bitgouel-api/src/main/kotlin/team/msg/common/init/DataInitializer.kt @@ -1,4 +1,4 @@ -package team.msg.common +package team.msg.common.init import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.event.EventListener