Java 使用AOP实现打印日志


@Aspect
@Component
@Slf4j
public class ControllerLogAspect {
    /**
     * 对所有的接口 添加日志 日志信息有 请求地址 被请求地址 请求参数
     *
     * @param joinPoint
     */
    @Before("@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.RestController)")
    public void restLogAccess(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String url = requestAttributes.getRequest().getRequestURI();
        String remoteAddr = requestAttributes.getRequest().getRemoteAddr();
        log.info("[WEB] remoteAddr: {},  URL: {}, args: {}", remoteAddr, url, joinPoint.getArgs());
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM