package com.gxzc.zen.logging.aop import com.fasterxml.jackson.databind.ObjectMapper import com.gxzc.zen.logging.annotation.LogAnnotation import com.gxzc.zen.logging.model.LogRequest import com.gxzc.zen.logging.util.MQLogUtil 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.MethodSignature import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.core.annotation.Order import org.springframework.stereotype.Component import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.context.request.RequestContextHolder import org.springframework.web.context.request.ServletRequestAttributes import java.util.* /** * * @author NorthLan * @date 2018/8/23 * @url https://noahlan.com */ @Suppress("unused") @Aspect @Order(1) @Component class LogRequestAspect { companion object { private val logger = LoggerFactory.getLogger(LogRequestAspect::class.java) } @Autowired private lateinit var mapper: ObjectMapper /** * 拦截所有 Controller结尾 的所有方法 */ @Pointcut("execution(* com.gxzc.zen..*Controller.*(..)))") fun controllerCut() { } @Around("controllerCut()") fun after(joinPoint: ProceedingJoinPoint): Any? { val method = (joinPoint.signature as MethodSignature).method val logAnnotation = method.getAnnotation(LogAnnotation::class.java) var remark: String? = null if (logAnnotation != null) { if (logAnnotation.ignore) { return joinPoint.proceed() } remark = logAnnotation.remark } val fullMethodName = method.declaringClass.name + "." + method.name var requestBody: Any? = null var i = -1 method.parameterAnnotations.forEach { annotations -> ++i annotations.forEach { if (it is RequestBody) { requestBody = joinPoint.args[i] } } } val attr = RequestContextHolder.getRequestAttributes() as? ServletRequestAttributes ?: return joinPoint.proceed() val request = attr.request val requestUri = request.requestURI val remoteHost = request.remoteHost val requestMethod = request.method val requestParam = request.queryString val data = LogRequest().apply { this.requestUri = requestUri this.requestMethod = requestMethod this.requestParam = requestParam this.requestBody = mapper.writeValueAsString(requestBody) // this.responseBody = mapper.writeValueAsString(result) this.remoteHost = remoteHost this.method = fullMethodName // this.exMsg = exMsg this.remark = remark } val startTime = Date() return try { val result = joinPoint.proceed() data.apply { this.responseBody = mapper.writeValueAsString(result) this.operatorStartTime = startTime this.operatorEndTime = Date() } MQLogUtil.logRequest(data) result } catch (e: Throwable) { // 运行时异常(保留5最近异常栈) var exMsg = e.javaClass.name + " : " + e.message + "\r\n" run breaking@{ e.stackTrace.forEachIndexed { index, it -> if (index > 4) { return@breaking } exMsg += it exMsg += "\r\n" } } exMsg += "......" data.apply { this.exMsg = exMsg this.operatorStartTime = startTime this.operatorEndTime = Date() } MQLogUtil.logRequest(data) throw e } } }