面向切面日志记录
import lombok.extern.slf4j.Slf4j
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.AfterReturning
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.springframework.stereotype.Component
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import javax.servlet.http.HttpServletRequest
@Aspect
@Component
@Slf4j
public class HttpAspect {
//定义切点
@Pointcut("execution(* org.hshk.controller.v1..*.*(..))")
public void log() {
}
//记录入参参数
@Before("log()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes()
HttpServletRequest httpServletRequest = attributes.getRequest()
//url
log.info("url={}", httpServletRequest.getRequestURL())
//method
log.info("method={}", httpServletRequest.getMethod())
//类方法
log.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())
//参数
log.info("args={}", joinPoint.getArgs())
}
//记录返回值
@AfterReturning(returning = "object", pointcut = "log()")
public void doAfterReturning(Object object) {
log.info("response={}", object.toString())
}
}