springboot AOP 日志,注解 保存 (2020-06-28 15:15)

接着上一篇,上一篇将日志输出,但是无法保存,下面针对日志保存进行说明,本次使用注解模式进行日志记录,只有注入对应的注解才可以保存该方法的日志

注:文章内容为参考开源代码,此处只是记录,非原创

注解声明

package com.****.clouddocommon.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
}

注解处理

package com.****.aspect;

import com.****.annotation.Log;
import com.****.context.FilterContextHandler;
import com.*****.dto.LogDO;
import com.*****.service.LogRpcService;
import com.****.utils.HttpContextUtils;
import com.*****.utils.IPUtils;
import com.*****.utils.JSONUtils;
import com.*****.utils.SecuityUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Aspect
@Component
public class LogAspect {
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

  //日志保存service @Autowired LogRpcService logService;   //如果不想使用注解形式,可以参考上一篇文章使用execution @Pointcut(
"@annotation(com.bootdo.clouddocommon.annotation.Log)") public void logPointCut() { } @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); // 执行方法 Object result = point.proceed(); // 执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; //异步保存日志 saveLog(point, time); return result; } void saveLog(ProceedingJoinPoint joinPoint, long time) throws InterruptedException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); LogDO sysLog = new LogDO(); Log syslog = method.getAnnotation(Log.class); if (syslog != null) { // 注解上的描述 sysLog.setOperation(syslog.value()); } // 请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); // 请求的参数 Object[] args = joinPoint.getArgs(); try { String params = JSONUtils.beanToJson(args[0]).substring(0, 4999); sysLog.setParams(params); } catch (Exception e) { } // 获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); // 设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); // 用户名 sysLog.setUserId(SecuityUtils.getCurrentUser().getId()); sysLog.setUsername(SecuityUtils.getCurrentUser().getUsername()); sysLog.setTime((int) time); // 系统当前时间 Date date = new Date(); sysLog.setGmtCreate(date); // 保存系统日志 logService.save(sysLog); } }

注:保存日志的方法,请自己写,数据库自行定义,可参考下面进行设计

 应用示例

    @Log("获取证照附件列表")//日志使用示例
    @GetMapping
    R listByPage(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<CertificateFileDO> list = certificateFileService.list(query);
        int total = certificateFileService.count(query);
        PageUtils pageUtil = new PageUtils(list, total);
        return R.ok().put("page",pageUtil);
    }

猜你喜欢

转载自www.cnblogs.com/yutf/p/13203121.html