【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和大数据问题,转载请注明作者和原文链接,感谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值