spring boot aop日志

					**spring aop 自定义注解方式实现日志管理**

需求:spring boot中请求一些方法时需插入操作日志到日志表中。
原来的处理方式:在每个需要插入日志的方法里封装日志实体然后调用日志保存方法
现在处理方式:spring aop 自定义注解方式实现日志管理
依赖:

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

定义切面类:

package com.mmys.crm.config.aop;

import com.alibaba.fastjson.JSON;
import com.mmys.crm.entity.common.OperationLog;
import com.mmys.crm.service.OperationLogDetail;
import com.mmys.crm.service.common.IOperationLogService;
import com.mmys.crm.utils.WebUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author EDZ
 */
@Aspect
@Component
public class WebLogAcpect {

    private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);

    private IOperationLogService logService;

    @Autowired
    public void setUserDao (IOperationLogService logService) {
        this.logService = logService;
    }

    /**
     * 定义切入点,切入点为加了OperationLogDetail注解的方法
     * 也可用execution(public * com.mmys.service..*.save*(..)) || execution(public * com.mmys.service..*.delete*(..))的方式打到相同目的
     */
    @Pointcut("@annotation(com.mmys.crm.service.OperationLogDetail)")
    public void webLog(){}

    /**
     * 前置通知:在连接点之前执行的通知
     * @param joinPoint
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null){
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            logger.info("URL : " + request.getRequestURL().toString());
            logger.info("HTTP_METHOD : " + request.getMethod());
            logger.info("IP : " + request.getRemoteAddr());
            logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
        }
    }
    /*
	 * 环绕增强
     * @param joinPoint
     */
    @Around("webLog()")
    public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{
        try {
            //方法执行完成后增加日志
            addOperationLog(joinPoint);
        }catch (Exception e){
            System.out.println("LogAspect 操作失败:" + e.getMessage());
            e.printStackTrace();
        }
    }

    @AfterReturning(returning = "ret",pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        logger.info("RESPONSE : " + ret);
    }

    @After("webLog()")
    public void doAfter(JoinPoint joinPoint) throws Throwable{
        logger.info("方法执行完毕");
    }

    /**
     * 操作日志对象
     * @param joinPoint
     */
    private void addOperationLog(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
        if(annotation != null){
            OperationLog log = new OperationLog();
            log.setUserId(WebUtils.loginUser().getId());
            log.setType(annotation.operationType().getValue());
            log.setContent(getContent(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));
            log.setBusiness(annotation.business());
            log.setCreateTime(new Date());
            logService.save(log);
        }
    }
    /**
     * 对当前登录用户和占位符处理
     * @param argNames 方法参数名称数组
     * @param args 方法参数数组
     * @param annotation 注解信息
     * @return 返回处理后的描述
     */
    private String getContent(String[] argNames, Object[] args, OperationLogDetail annotation){

        Map<Object, Object> map = new HashMap<>(4);
        for(int i = 0;i < argNames.length;i++){
            map.put(argNames[i],args[i]);
        }
        String content = annotation.content();
        try {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Object k = entry.getKey();
                Object v = entry.getValue();
                content = content.replace("{" + k + "}", JSON.toJSONString(v));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }
}

service层添加OperationLogDetail 注解

	/**
     * 添加OperationLogDetail注解
     */
@OperationLogDetail(business = "款项合并",operationType = LogType.UPDATE,content = "合并的款项id:{ids}")
    @Override
    public String saveMergeMoney(List<Long> ids) {
        String returnMsg = "";
        return ReturnVO.error(returnMsg);
    }

自定义注解:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {

    String business() default "";
    /**
     * 方法描述,可使用占位符获取参数:{{tel}}
     */
    String content() default "";

    /**
     * 操作类型(enum):主要是select,insert,update,delete
     */
    LogType operationType() default LogType.INSERT;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值