JAVA代码使用SpringBoot AOP实现操作系统日志

本文介绍了如何在Java项目中使用SpringBoot的AOP功能来实现系统日志管理,通过创建日志注解、切面类以及相关业务逻辑类,实现日志的自动记录,减少了代码冗余。详细内容包括操作日志表的设计、注解定义、切面类的编写以及相关辅助类的说明。
摘要由CSDN通过智能技术生成

这两天做了个系统日志管理功能,现在来总结一下。项目中用到AOP主要是将日志记录从业务逻辑代码中划分出来,减少冗余代码和重复工作步骤...废话有点多

首先创建操作系统日志表,表结构如下:

1.创建操作日志注解类Log.java

package com.daqing.financial.hrauth.annotation;


import com.daqing.financial.hrauth.enums.OperationType;
import com.daqing.financial.hrauth.enums.OperationUnit;

import java.lang.annotation.*;

/**
 * @author Rogers
 * 操作日志注解
 * @create 2020-07-03
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

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

    /**
     * 日志等级:自己定,此处分为1-9
     */
    int level() default 0;

    /**
     * 操作类型(enum):主要是select,insert,update,delete
     */
    OperationType operationType() default OperationType.UNKNOWN;

    /**
     * 被操作的对象(此处使用enum):可以是任何对象,如表名(user),或者是工具(redis)
     */
    OperationUnit operationUnit() default OperationUnit.UNKNOWN;


}


2.创建切面类记录操作系统日志(此处的获取登录用户信息是根据request请求中的token获取到user_id,从而得到所有用户信息)

package com.daqing.financial.hrauth.aspect;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daqing.financial.hrauth.annotation.Log;
import com.daqing.financial.hrauth.service.TokenService;
import com.daqing.financial.hrauth.service.UserLoginService;
import com.daqing.framework.domain.hrms.Token;
import com.daqing.framework.domain.hrms.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SysLogAspect
 * @Description 操作日志切面
 * @Date 2020/9/30 
 * @Version 1.0
 */
@Slf4j
@Aspect
@Component
public class SysLogAspect {

    @Resource
    private Operation operation;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserLoginService userLoginService;
    /**
     * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
     * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'
     */
    @Pointcut("@annotation(com.daqing.financial.hrauth.annotation.Log)")
    public void operationLog() {
    }


    /**
     * 环绕增强,相当于MethodInterceptor
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = null;
        long time = System.currentTimeMillis();
        try {
            res = joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            return res;
        } finally {
            try {
                //User systemUser = (User) SecurityUtils.getSubject().getPrincipal();
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                String token = request.getHeader("token");
                Token userToken = tokenService.getOne(new QueryWrapper<Token>().eq("token", token));
                UserEntity systemUser = userLoginService.getOne(new QueryWrapper<UserEntity>().eq("id",userToken.getUserId()));
                operation.addOperationLog(joinPoint,res,time,systemUser);
                //方法执行完成后增加日志
//                addOperationLog(joinPoint, res, time);
            } catch (Exception e) {
                log.error("LogAspect 操作失败:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }


    /**
     * 对当前登录用户和占位符处理
     *
     * @param argNames   方法参数名称数组
     * @param args       方法参数数组
     * @param annot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值