【SpringBoot】AOP-日誌記錄

前言

每個controller的訪問我們都可以記錄一些日誌信息和異常信息方便我們排查問題。

但是關鍵的日誌信息,還是需要自己去手動添加。

Ready

  1. maven aop包的引入
  2. AOP切面基礎知識

Github

地址:https://github.com/ithuhui/hui-base-java
分支:master
模塊:【hui-base-common】
位置:com.hui.base.common.aspect

Code

Maven

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

Web日誌切面

/**
 * <b><code>WebLogAspect</code></b>
 * <p/>
 * Description: 日誌收集
 * <p/>
 * <b>Creation Time:</b> 2018/12/4 22:14.
 *
 * @author HuWeihui
 */
@Component
@Aspect
@Slf4j
public class WebLogAspect {

    /**
     * 定義controller層切面.
     *
     * @author HuWeihui
     * @since hui_project v1
     */
    @Pointcut(value = "execution(public * com.richstonedt.nile.szcst.rs.controller..*.*(..))")
    public void webControllerLog() {
    }

    /**
     * 定義service層切面.
     *
     * @author HuWeihui
     * @since hui_project v1
     */
    @Pointcut(value = "execution(public * com.richstonedt.nile.szcst.cs.service..*.*(..))")
    public void webServiceLog() {
    }

    /**
     * 前置通知 記錄controller調用的日誌.
     *
     * @param joinPoint the join point
     * @author HuWeihui
     * @since hui_project v1
     */
    @Before(value = "webControllerLog()")
    public void controllerBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info("\n");
        log.info("-------------------Request Content-------------------");
        log.info("[Request IP] : {}", request.getRemoteAddr());
        log.info("[Request URL] : {} ", request.getRequestURL());
        log.info("[Request Method] : {}", request.getMethod());
        log.info("[Class Method] : {}", joinPoint.getSignature());
        Object[] args = joinPoint.getArgs();
        if (args == null) {
            log.info("[Args is NULL] : {}", "NULL");
        } else {
            log.info("[Class Method Args] : " + Arrays.toString(args));
        }
        log.info("-------------------Request Content-------------------");
        log.info("\n");
    }

    /**
     * 前置通知 記錄service調用的日誌.
     *
     * @param joinPoint the join point
     * @author HuWeihui
     * @since hui_project v1
     */
    @Before(value = "webServiceLog()")
    public void serviceBefore(JoinPoint joinPoint) {
        log.info("\n");
        log.info("-------------------Service Content-------------------");
        log.info("[Service Class] : {}",
                joinPoint.getTarget().getClass().getName());
        log.info("[Service Method] : {}", joinPoint.getSignature().getName());
        log.info("[Service Params] : {}", joinPoint.getSignature());
        Object[] args = joinPoint.getArgs();
        if (args == null) {
            log.info("[Service Args] is NULL : {}", "NULL");
        } else {
            log.info("[Service Method] Args : " + Arrays.toString(args));
        }
        log.info("-------------------Service Content-------------------");
        log.info("\n");
    }

    /**
     * 異常通知 記錄controller拋異常的信息.
     *
     * @param joinPoint the join point
     * @param error the error
     * @author HuWeihui
     * @since hui_project v1
     */
    @AfterThrowing(value = "webControllerLog()", throwing = "error")
    public void controllerAfterThrowing(JoinPoint joinPoint, Throwable error) {
        log.info("\n");
        log.error(
                "-------------------Controller Throwable Content-------------------");
        log.error("[Controller Throwable Class] : {}",
                error.getClass().getName());
        log.error("[Controller Throwable Msg] : {}", error.getMessage());
        log.error("[Controller Throwable Method] : {}->{}()",
                joinPoint.getTarget().getClass().getName(),
                joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        if (args == null) {
            log.info("[Service Args] is NULL : {}", "NULL");
        } else {
            log.info("[Service Method] Args : " + Arrays.toString(args));
        }
        log.error("[Controller Throwable Method Args] : {}",
                joinPoint.getArgs());
        log.error(
                "-------------------Controller Throwable Content-------------------");
        log.info("\n");
    }
}

在【hui-base-common】下面的 com.hui.base.common.aspect

作者

 作者:HuHui
 轉載:歡迎一起討論web和大數據問題,轉載請註明作者和原文鏈接,感謝
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章