自定义日志注解,保存信息到数据库

定义日志注解

import java.lang.annotation.*;

/**
 * @author wzw
 * @version 1.0
 * @Date 2023-2-17 17:31:19
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

    /**
     * 描述
     * @return
     */
    String desc();


    boolean isSaveLog() default true;

}

定义切面

定义切面,在使用了指定注解的方法执行前后,进行操作,这里是用来在使用了自定义注解的方法执行完,保存日志信息
UserBean是测试系统的用户实体,BusinessLogs 是测试系统的日志实体,直接删掉就可以了

import com.alibaba.fastjson.JSON;
import com.weeon.platform.common.bean.UserBean;
import com.weeon.platform.common.dao.BusinessLogsDao;
import com.weeon.platform.common.domain.BusinessLogs;
import com.weeon.platform.common.utils.HttpRequestUtils;
import com.weeon.platform.common.utils.PrimaryKeyUtils;
import com.weeon.platform.common.utils.WebUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author wzw
 * @version 1.0
 * @Date 2023-2-17 17:31:19
 */
@Aspect
@Component
public class LogPoint {

    @Resource
    private BusinessLogsDao businessLogsDao;

    //配置织入点
    @Pointcut("@annotation(com.weeon.platform.api.log.Log)")
    public void logPointCut() {

    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint  切点
     * @param jsonResult 返回的信息
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterRetuning(JoinPoint joinPoint, Object jsonResult) {
        Log annotationLog = getAnnotationLog(joinPoint);
        if(annotationLog.isSaveLog()){
            handleLog(joinPoint, null, jsonResult);
        }
    }

    /**
     * 处理完请求之后执行
     *
     * @param joinPoint  切点
     * @param e          异常信息
     * @param jsonResult 返回结果
     */
    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {

        UserBean userBean = WebUserUtils.getUser();

        Log controllerLog = getAnnotationLog(joinPoint);
        if (controllerLog == null) {
            return;
        }

        BusinessLogs sysLog = new BusinessLogs();
        sysLog.setId(PrimaryKeyUtils.getId());

        // 请求的地址
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) attributes;
        HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();

        //请求参数
        Map<String, String[]> param = ((ServletRequestAttributes) attributes).getRequest().getParameterMap();
        String s = JSON.toJSONString(param);
        s=s.replaceAll("(\\[)|(\\])","");
        sysLog.setParvalue(s);
        sysLog.setUrl(HttpRequestUtils.getRequestUrl(request) + " " + request.getMethod());

        //操作用户信息
        sysLog.setUserid(userBean.getUserid());
        sysLog.setUsername(userBean.getUsername());
        sysLog.setZtid(userBean.getZtid());
        sysLog.setZtcode(userBean.getZtcode());
        sysLog.setZtname(userBean.getZtname());
        String ip = HttpRequestUtils.getIp(request);
        sysLog.setIp(ip);

        // 设置方法名称
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        sysLog.setClassname("类名:" + className + " 方法:" + methodName + "()");

        //日志描述
        sysLog.setDescription(controllerLog.desc());

        // 保存到数据库
        businessLogsDao.save(sysLog);

    }

    /**
     * 是否存在注解,如果存在就获取
     *
     * @param joinPoint 切点
     * @return 注解对象
     */
    private Log getAnnotationLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(Log.class);
        }
        return null;
    }


}

使用

    @Log(desc = "访问了/testhandler/test1路径",isSaveLog = false)
    @RequestMapping("/testhandler/test1")
    public String test1(){


        return "123";
    }

在这里插入图片描述

其它

获取调用者类名和方法名

        String className = new Exception().getStackTrace()[1].getClassName(); //获取调用者的类名
        String methodName = new Exception().getStackTrace()[1].getMethodName(); //获取调用者的方法名

切点

  • 使用@annotation(annotation)作为切点表达式,表示拦截所有带有指定注解的方法。
@AfterReturning(pointcut = "@annotation(Log)")
public void doAfterReturning(JoinPoint joinPoint, Log log) {
    // 处理日志
}
  • 使用@target(target)作为切点表达式,表示拦截所有指定类型的方法。
@AfterReturning(pointcut = "@target(Controller)")
public void doAfterReturning(JoinPoint joinPoint, Controller controller) {
    // 处理日志
}
  • 使用@within(within)作为切点表达式,表示拦截所有指定类型的类的方法。
@AfterReturning(pointcut = "@within(Service)")
public void doAfterReturning(JoinPoint joinPoint, Service service) {
    // 处理日志
}
  • 使用@args(args)作为切点表达式,表示拦截所有带有指定参数的方法。
@AfterReturning(pointcut = "@args(String)")
public void doAfterReturning(JoinPoint joinPoint, String arg) {
    // 处理日志
}
  • 使用@annotation(annotation)和@target(target)、@within(within)、@args(args)组合使用,表示拦截所有带有指定注解、指定类型、指定类型的类的方法、带有指定参数的方法。
@AfterReturning(pointcut = "@annotation(Log) && @target(Controller) && @args(String)")
public void doAfterReturning(JoinPoint joinPoint, Log log, Controller controller, String arg) {
    // 处理日志
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值