-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLogAspect.kt
43 lines (38 loc) · 1.45 KB
/
LogAspect.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.example.advice
import mu.KotlinLogging
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.stereotype.Component
private val logger = KotlinLogging.logger { }
@Aspect
@Component
class LogAspect {
@Suppress("TooGenericExceptionCaught")
@Around("@annotation(com.example.annotation.LogExecution)")
fun logExecutionTime(joinPoint: ProceedingJoinPoint): Any {
val start = System.currentTimeMillis()
val signature = joinPoint.signature.toShortString()
val result = try {
with(StringBuilder("start -> Executing $signature, "), {
appendParameters(joinPoint.args)
logger.info(toString())
})
joinPoint.proceed()
} catch (throwable: Throwable) {
logger.error("*** Exception during executing $signature,", throwable)
throw throwable
}
val duration = System.currentTimeMillis() - start
logger.info("end -> Finished executing: $signature, returned: '$result', duration: $duration ms")
return result
}
private fun StringBuilder.appendParameters(args: Array<Any>) {
append("parameters: [")
args.forEachIndexed { i, p ->
append(p.javaClass.simpleName).append("(").append(p.toString()).append(")")
if (i < args.size - 1) append(", ")
}
append("]")
}
}