Skip to content

Commit

Permalink
feat : log aop 구현 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
imenuuu committed Nov 25, 2023
1 parent 913cffb commit d231e92
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/main/kotlin/com/example/cmc_be/common/aop/LogAspect.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.cmc_be.common.aop

import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.*
import org.aspectj.lang.reflect.MethodSignature
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.lang.reflect.Method

@Component
@Aspect
class LogAspect{
@Pointcut("execution(* com.example.cmc_be..*Controller.*(..))")
fun controller() {
}


@Pointcut("execution(* com.example.cmc_be..*Service.*(..))")
fun service() {
}


@Before("controller()")
@Throws(Throwable::class)
fun beforeLogic(joinPoint: JoinPoint) {
val methodSignature = joinPoint.signature as MethodSignature
val method = methodSignature.method
val methodName = getMethodName(method)
log.info("==========================LOG_START==========================")
log.info("logging start method = {}", methodName)
val parameterNames = methodSignature.parameterNames
val args = joinPoint.args
var index = 0
for (arg in args) {
if (arg != null) {
log.info(
"parameterNames = {} type = {}, value = {}",
parameterNames[index], arg.javaClass.simpleName, arg.toString()
)
}
index += 1
}
}

private fun getMethodName(method: Method): String {
return method.name
}

@After("controller()")
@Throws(Throwable::class)
fun afterLogic(joinPoint: JoinPoint) {
val method = getMethod(joinPoint)
val methodName = getMethodName(method)
log.info("logging finish method = {}", methodName)
log.info("==========================LOG_FINISH==========================")
}

@AfterReturning(value = "controller()", returning = "returnObj")
fun afterReturnLog(joinPoint: JoinPoint, returnObj: Any?) {
val method = getMethod(joinPoint)
val methodName = getMethodName(method)
log.info("========================RETURN_LOG============================")
log.info("method name = {}", methodName)
if (returnObj != null) {
log.info("return type = {}", returnObj.javaClass.simpleName)
}
log.info("return value = {}", returnObj.toString())
log.info("==============================================================")
}


private fun getMethod(joinPoint: JoinPoint): Method {
val signature = joinPoint.signature as MethodSignature
return signature.method
}

companion object {
private val log = LoggerFactory.getLogger(LogAspect::class.java)
}



}

0 comments on commit d231e92

Please sign in to comment.