SpringBoot AOP 日志记录

1.首先引入依赖,我的2.2.5版本需要引入,但是另一个2.1.5版本是不需要引入就可以使用注解


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

2.创建一个自定义注解类

import java.lang.annotation.*;

/**
 * @Auther: DELL
 * @Date: 2020/3/26 10:17
 * @Description:
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Log {
    String value() default "";
}

3.创建我们的AOP配置class

import com.example.demo.dao.WebBankLogDao;
import com.example.demo.entity.ebbanklog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Auther: DELL
 * @Date: 2020/3/26 10:06
 * @Description:
 */
@Aspect
@Component
public class myAspect {
    @Autowired
    private WebBankLogDao webBankLogDao;
    @Pointcut("@annotation(com.example.demo.aspect.Log)")
    public void pointcut() { }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        Object result = null;
        long beginTime = System.currentTimeMillis();
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        // 保存日志
        saveLog(point, time);
        //System.out.println("point=="+point+";time"+time+"Session"+session.getAttribute("loginUser"));
        return result;
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        ebbanklog sysLog = new ebbanklog();
        Log logAnnotation = method.getAnnotation(Log.class);
        if (logAnnotation != null) {
            // 注解上的描述
            sysLog.setOption(logAnnotation.value());
        }
        // 请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");
        // 请求的方法参数值
        Object[] args = joinPoint.getArgs();
        // 请求的方法参数名称
        LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
        String[] paramNames = u.getParameterNames(method);
        if (args != null && paramNames != null) {
            String params = "";
            for (int i = 0; i < args.length; i++) {
                params += " " + paramNames[i] + ":" + args[i];
            }
            sysLog.setParams(params);
        }
        // 获取request
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        HttpSession session=request.getSession();

        // 设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));
        // 模拟一个用户名
        sysLog.setUsercode(session.getAttribute("userCode").toString());
        sysLog.setRuntime(Long.toString(time));
        sysLog.setCreatetime(new Date());
        // 保存系统日志
        webBankLogDao.save(sysLog);
    }

4.在需要记录的方法地方加上我们的自定义注解,不局限于Controller

SpringBoot AOP 日志记录_第1张图片

5.日志记录在数据库中

建表语句

create table WEBBANKLOG
(
  ID         NUMBER(12) not null,
  USERCODE   VARCHAR2(30),
  OPTIONS    VARCHAR2(200),
  RUNTIME    NUMBER,
  METHOD     VARCHAR2(200),
  PARAMS     VARCHAR2(2000),
  IP         VARCHAR2(200),
  CREATETIME DATE
)

 

你可能感兴趣的:(SPRING)