add aop logs

This commit is contained in:
rainerosion 2021-06-30 23:57:29 +08:00
parent aa1096e8c2
commit dcb6991de9
2 changed files with 40 additions and 3 deletions

View File

@ -28,6 +28,7 @@ dependencies {
implementation("org.postgresql:postgresql:42.2.18") implementation("org.postgresql:postgresql:42.2.18")
implementation("org.projectlombok:lombok:1.18.12") implementation("org.projectlombok:lombok:1.18.12")
implementation("org.projectlombok:lombok:1.18.12") implementation("org.projectlombok:lombok:1.18.12")
implementation("com.alibaba:fastjson:1.2.73")
} }
tasks.withType<KotlinCompile> { tasks.withType<KotlinCompile> {

View File

@ -1,10 +1,15 @@
package email.timemail.mail.config package email.timemail.mail.config
import com.alibaba.fastjson.JSONObject
import lombok.extern.slf4j.Slf4j import lombok.extern.slf4j.Slf4j
import org.aspectj.lang.JoinPoint 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.Aspect
import org.aspectj.lang.annotation.Before import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut import org.aspectj.lang.annotation.Pointcut
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import org.springframework.web.context.request.RequestContextHolder import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes import org.springframework.web.context.request.ServletRequestAttributes
@ -13,13 +18,44 @@ import org.springframework.web.context.request.ServletRequestAttributes
@Component @Component
@Aspect @Aspect
open class LogAspect { open class LogAspect {
@Pointcut("execution(public * email.timemail.controller...*Controller.*(..))") private val log: Logger = LoggerFactory.getLogger(LogAspect::class.java)
open fun webLog(): Unit {}
@Pointcut("execution(public * email.timemail.mail.controller.*Controller.*(..))")
open fun webLog(): Unit {
}
@Before("webLog()") @Before("webLog()")
open fun doBefore(joinPoint: JoinPoint): Unit { open fun doBefore(joinPoint: JoinPoint): Unit {
var attributes: ServletRequestAttributes = RequestContextHolder.getRequestAttributes() as ServletRequestAttributes var attributes: ServletRequestAttributes =
RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
var request = attributes.request var request = attributes.request
// 打印请求相关参数
log.info("========================================== Start ==========================================")
// 打印请求 url
log.info("[请求URL] : {}", request.getRequestURL().toString())
// 打印 Http method
log.info("[请求方法] : {}", request.getMethod())
// 打印调用 controller 的全路径以及执行方法
log.info(
"[请求类名] : {}.{}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName()
);
// 打印请求的 IP
log.info("[请求IP] : {}", request.getRemoteAddr())
// 打印请求入参
log.info("[请求参数] : {}", JSONObject.toJSONString(joinPoint.getArgs()))
}
@Around("webLog()")
open fun doAround(proceedingJoinPoint: ProceedingJoinPoint): Any {
var startTime = System.currentTimeMillis()
var result = proceedingJoinPoint.proceed()
// 打印出参
log.info("[响应结果] : {}", JSONObject.toJSONString(result))
// 执行耗时
log.info("[请求耗时] : {} ms", System.currentTimeMillis() - startTime);
log.info("=========================================== End ===========================================")
return result;
} }
} }