springboot aop日志记录

一、POM增加AOP JAR包

org.springframework.boot
spring-boot-starter-aop
2.0.4.RELEASE


二、Logweb类
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Logweb
{
    String value() default "";
}


三、SysAspect类
package cn.com.hfai.controller.system;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Aspect
@Component
public class SysAspect
{
    private static final Logger logger = LoggerFactory.getLogger(SysAspect.class);
   
    ThreadLocal startTime = new ThreadLocal<>();
           
    @Pointcut("@annotation(cn.com.hfai.controller.system.Logweb)")
    public void LogPointcut(){}

    @Before("LogPointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable
    {
        startTime.set(System.currentTimeMillis());
        logger.info("====>>>>SysAspect doBefore");

        ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attribute.getRequest();
        logger.info("====>>>>request startTime::" + new Date());
        logger.info("====>>>>request URL::" + request.getRequestURL().toString());
        logger.info("====>>>>request method::" + request.getMethod());
        logger.info("====>>>>request IP::" + request.getRemoteAddr());
        logger.info("====>>>>request class method={}",joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        logger.info("====>>>>request method paramter={}", joinPoint.getArgs());

    }

    @AfterReturning(returning = "ret", pointcut = "LogPointcut()")
    public void doAfterReturning(Object ret) throws Throwable
    {
        logger.info("====>>>>SysAspect doAfterReturning response::" + ret);
        logger.info("====>>>>response endTime::" + new Date());
        logger.info("====>>>>spend time::" + (System.currentTimeMillis()-startTime.get()));
    }
}

注:@annotation(cn.com.hfai.controller.system.Logweb) 一定要指定Logweb类

四、在Controller类的方法之上加上注解 @Logweb 即可

最后测试结果:
2018-12-10 19:34:59.929  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>SysAspect doBefore
2018-12-10 19:34:59.929  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request startTime::Mon Dec 10 19:34:59 CST 2018
2018-12-10 19:34:59.930  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request URL::http://localhost:8080/auth/userLogin
2018-12-10 19:34:59.930  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request method::POST
2018-12-10 19:34:59.930  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request IP::0:0:0:0:0:0:0:1
2018-12-10 19:34:59.931  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request class method=cn.com.hfai.controller.user.UserController.userLogin
2018-12-10 19:34:59.931  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>request method [email protected]
2018-12-10 19:34:59.931  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>SysAspect doAfterReturning response::ModelAndView: reference to view with name 'home'; model is null
2018-12-10 19:34:59.931  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>response endTime::Mon Dec 10 19:34:59 CST 2018
2018-12-10 19:34:59.931  INFO 11640 --- [nio-8080-exec-5] cn.com.hfai.controller.system.SysAspect  : ====>>>>spend time::2


注:这个只是打印在控制台上,若想放到数据库中,则需要增加操作数据库的业务代码



已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



你可能感兴趣的:(springboot,aop,日志)