spring-boot使用aop进行日志记录

2020-09-24 15:53:18 浏览数 (1)

使用aop在项目中进行日志记录,很适合aop的应用场景

使用aop进行日志记录

环境搭建

创建一个spring boot项目,并引入spring aop

项目中的pom.xml内容为

代码语言:javascript复制
<dependencies>

       <!-- SpringBoot 拦截器 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-aop</artifactId>
       </dependency>

       <!-- SpringBoot Web容器 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-lang3</artifactId>
           <version>${commons-lang3.version}</version>
       </dependency>


       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatisplus-spring-boot-starter</artifactId>
           <version>${mybatisplus-spring-boot-starter.version}</version>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus</artifactId>
           <version>${mybatis-plus.version}</version>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-generate</artifactId>
           <version>${mybatis-plus.version}</version>
       </dependency>

       <!--阿里数据库连接池 -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid-spring-boot-starter</artifactId>
           <version>${druid.version}</version>
       </dependency>

       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>${fastjson.version}</version>
       </dependency>

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

   </dependencies>

编写自定义日志注解

在合适的包下创建自定义注解BussinessLog

代码语言:javascript复制
 /**
 * 标记需要做业务日志的方法
 *
 * @author earthchen
 * @date 2018/8/24
 **/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BussinessLog {

    /**
     * 日志类型
     *
     * @return
     */
    String type() default "";

    /**
     * 业务的名称,例如:"修改菜单"
     */
    String value() default "";


}

如果还需要其他的参数可以自定义其他方法

编写日志逻辑

创建一个日志切面

代码语言:javascript复制
import com.alibaba.fastjson.JSONObject;
import com.earthchen.constant.BusinessStatus;
import com.earthchen.domain.OperationLog;
import com.earthchen.log.AsyncFactory;
import com.earthchen.log.LogManager;
import com.earthchen.log.annotation.BussinessLog;
import com.earthchen.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

/**
 * 日志切面
 *
 * @author earthchen
 * @date 2018/8/24
 **/
@Aspect
@Component
@EnableAsync
public class LogAop {

    private Logger log = LoggerFactory.getLogger(this.getClass());


    @Pointcut(value = "@annotation(com.earthchen.log.annotation.BussinessLog)")
    public void logPointCut() {
    }

    /**
     * 前置通知 用于拦截操作
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "logPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        handleLog(joinPoint, null);
    }

    /**
     * 拦截异常操作
     *
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfter(JoinPoint joinPoint, Exception e) {
        handleLog(joinPoint, e);
    }

    @Async
    protected void handleLog(final JoinPoint joinPoint, final Exception e) {
        try {
            // 获得注解
            BussinessLog controllerLog = getAnnotationLog(joinPoint);
            if (controllerLog == null) {
                return;
            }

            // 获取当前的用户
//            User currentUser = ShiroUtils.getUser();

            // *========数据库日志=========*//
            OperationLog operLog = new OperationLog();
            operLog.setStatus(BusinessStatus.SUCCESS);
            operLog.setMessage("操作成功");

            operLog.setCreatetime(new Date());
            // 请求的地址

            operLog.setOperUrl(HttpUtil.getRequest().getRequestURI());


            if (e != null) {
                operLog.setStatus(BusinessStatus.FAIL);
                operLog.setMessage(StringUtils.substring(e.getMessage(), 0, 2000));
            }
            // 设置方法名称
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            operLog.setMethod(className   "."   methodName   "()");
            // 处理设置注解上的参数
            getControllerMethodDescription(controllerLog, operLog);
            // 保存数据库
            LogManager.me().executeLog(AsyncFactory.bussinessLog(operLog));
        } catch (Exception exp) {
            // 记录本地异常日志
            log.error("==前置通知异常==");
            log.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param log
     * @param operLog
     * @throws Exception
     */
    public void getControllerMethodDescription(BussinessLog log, OperationLog operLog) throws Exception {
        // 设置日志类型
        operLog.setLogtype(log.type());
        // 设置日志名字
        operLog.setLogname(log.value());
        // 获取参数的信息,传入到数据库中。
        setRequestValue(operLog);
    }

    /**
     * 获取请求的参数,放到log中
     *
     * @param operLog
     */
    private void setRequestValue(OperationLog operLog) {
        Map<String, String[]> map = HttpUtil.getRequest().getParameterMap();
        String params = JSONObject.toJSONString(map);
        operLog.setOperParams(params);
    }


    /**
     * 是否存在注解,如果存在就获取
     *
     * @param joinPoint
     * @return
     * @throws Exception
     */
    private BussinessLog getAnnotationLog(JoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null) {
            return method.getAnnotation(BussinessLog.class);
        }
        return null;
    }
}

  • 这里定义的切点定义是所有被BussinessLog注解的方法上,如果有其他需求也可以自定义
  • 这里还是用了@EnableAsync和@Async注解,使其在打日志的时候是异步的
  • 由于异步交给线程池处理,在线程中不能直接获取spring中的bean,所以需要借助springUtil获取相关bean进行操作
代码语言:javascript复制
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * spring工具类 方便在非spring管理环境中获取bean
 *
 * @author earthchen
 * @date 2018/8/24
 **/
@Component
public final class SpringUtils implements BeanFactoryPostProcessor {
    /**
     * Spring应用上下文环境
     */
    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }
}

编写controller进行测试

代码语言:javascript复制
import com.earthchen.log.annotation.BussinessLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author earthchen
 * @date 2018/8/24
 **/
@RestController
public class TestController {

    @BussinessLog(type = "操作",value = "进行测试")
    @RequestMapping("/test")
    public String testLog(){
        return "test";
    }
}

运行项目,然后访问上述controller,然后查看控制台和数据库中相应的表是否有对应数据

项目地址:https://gitee.com/earthchen/aop-log-demo

0 人点赞