封装log日志

334 阅读1分钟

1、系统日志是通过Spring AOP实现的,我们自定义了注解 @SysLog ,且只能在方法上使用,如下所示:

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

下面是自定义注解 @SysLog 的使用方式,如下所示:

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @SysLog("保存用户")
    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user) {
	ValidatorUtils.validateEntity(user, AddGroup.class);
	Long userid = user.getUserId();
	if (userid == null) {
		user.setUserId(Long.valueOf(System.currentTimeMillis()));
	}
	user.setCreateUserId(getUserId());
	sysUserService.save(user);
	return R.ok();
    }
}

我们只需要在保存日志的请求方法上,加上 @SysLog 注解,就可以把日志保存到数据库中

2、具体在哪里处理数据保存在数据库中,定义了 SysLogAspect 处理类,就是来干这个事情的,如下所示:

/**
 * 系统日志,切面处理类
 */
@Aspect
@Component
public class SysLogAspect {
    @Autowired
    private SysLogService sysLogService;
    
    @Pointcut("@annotation(com.cn.annotation.SysLog)")
    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;
        //保存日志
        saveSysLog(point, time);
        return result;
    }
    private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
	MethodSignature signature = (MethodSignature) joinPoint.getSignature();
	Method method = signature.getMethod();
	SysLogEntity sysLog = new SysLogEntity();
	SysLog syslog = method.getAnnotation(SysLog.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 = new Gson().toJson(args[0]);
		sysLog.setParams(params);
	}catch (Exception e){

	}
	//获取request
	HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
	//设置IP地址
	sysLog.setIp(IPUtils.getIpAddr(request));
	//用户账号
	String usercode = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsercode();
	sysLog.setUsercode(usercode);
	sysLog.setTime(time);
	sysLog.setCreateDate(new Date());
	//保存系统日志
	sysLogService.insert(sysLog);
    }
}

SysLogAspect 类定义了一个切入点,请求 @SysLog 注解方法时,会进入 around 的方法,会把系统日志保存到数据库中。