Skip to content

Commit

Permalink
Merge pull request #44 from GSM-MSG/43-feat/http-logging-aspect
Browse files Browse the repository at this point in the history
#44 HttpLoggingAspect 클래스 작성
  • Loading branch information
esperar authored Oct 20, 2023
2 parents ce08db8 + f475db0 commit 8eebaa3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String> = 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<String,Any> = HashMap()

for (i in parameterNames.indices) {
params[parameterNames[i]] = args[i]
}
return params
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8eebaa3

Please sign in to comment.