【SpringBoot】AOP切面记录日志

版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85008928

前言

每个controller的访问我们都可以记录一些日志信息和异常信息方便我们排查问题。

但是关键的日志信息,还是需要自己去手动添加。

装备

  1. maven aop包的引入
  2. AOP切面基础知识

Core-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");
    }
}

github

https://github.com/ithuhui/hui-base-springboot/tree/master/hui-base-springboot-rest/src/main/java/com/hui/base/springboot/aspect

作者

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/85008928