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