spring aop 实现controller 日志

@Aspect
@Component
@Slf4j
public class ControllerAspact {

    @Pointcut("execution(public * com.example.controller..*.*(..))")
    public void requestLog() {
    }

    @Before("requestLog()")
    public void doBefore(JoinPoint joinPoint) {
        if (log.isDebugEnabled()) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            log.debug("url={}", request.getRequestURL());
            log.debug("method={}", request.getMethod());
            log.debug("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            log.debug("args={}", Arrays.toString(joinPoint.getArgs()));
        }
    }

    @AfterReturning(returning = "object", pointcut = "requestLog()")
    public void doAfterReturning(Object object) {
        log.info("response={}", object == null ? null : object.toString());
    }
}

猜你喜欢

转载自www.cnblogs.com/wilwei/p/10244703.html
今日推荐