AOP實現(xiàn)系統(tǒng)日志功能

1.pom文件
        <!-- Aop BEGIN -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- Aop END -->
2.自定義日志注解
/**
 * 
* @ClassName: SysLog
* @Description: 系統(tǒng)日志注解
* @author chenliqiao
* @date 2018年5月23日 上午10:12:46
*
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SysLog {
    
    /**
     * 模塊
     */
    String module() default "";
    
    /**
     * 操作類型
     */
    String type() default "";
    
    /**
     * 操作名稱
     */
    String name() default "";
}
3.Aspect日志切面
/**
 * 
* @ClassName: SysLogAspect  
* @Description: 系統(tǒng)日志切面
* @author chenliqiao
* @date 2018年5月23日 下午12:00:17
*
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class SysLogAspect {
    
    @Resource
    protected JsonRedisUtil redisUtil;
    
    @Resource
    private SysLogInfoService sysLogInfoService;
    
    /**
     * 注解聲明切點
     */
    @Pointcut("@annotation(cn.net.infinite.amms.common.annotation.SysLog)")
    public void annotationPointCut(){       
    }
    
    /**
     * 環(huán)繞通知
     */
    @SuppressWarnings("unchecked")
    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result=joinPoint.proceed();
        
        //方法執(zhí)行失敗,則直接返回
        Result<Object> response=(Result<Object>) result;
        if(!ResultCode.SUCCESS_CODE.equals(response.getRetCode()))
            return result;
        
        //保存系統(tǒng)日志
        this.saveSysLog(joinPoint, result);
        
        return result;
    } 
    
    /**
     * 保存系統(tǒng)日志信息
     */
    private void saveSysLog(ProceedingJoinPoint joinPoint,Object result){
        SysLogInfo entity=new SysLogInfo();
        
        //設(shè)置操作人信息
        CurrentUserInfo currentUser=this.redisUtil.string_get(AdminInfoUtil.getCurrentToken(), CurrentUserInfo.class);
        if(currentUser!=null){
            entity.setOperatorId(currentUser.getBaseInfo().getId());
            entity.setOperator(currentUser.getBaseInfo().getName());
        }
        entity.setOperatorTime(new Date());
        
        //獲取系統(tǒng)日志注解,并設(shè)置操作類型和操作描述
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        SysLog sysLog=signature.getMethod().getAnnotation(SysLog.class);
        entity.setModule(sysLog.module());
        entity.setType(sysLog.type());
        entity.setTitle(sysLog.name());
        
        //請求參數(shù)和返回結(jié)果
        List<Object> params=new ArrayList<>();
        for (Object param : joinPoint.getArgs()) {
            //上傳文件格式,只記錄文件名
            if(param instanceof MultipartFile){
                MultipartFile file=(MultipartFile) param;
                params.add(file.getOriginalFilename());
                continue;
            }
            params.add(param);  
        }
        entity.setReqParam(JsonUtil.beanToJson(params));
        entity.setResult(JsonUtil.beanToJson(result));
        
        //持久化到庫
        this.sysLogInfoService.add(entity);
    }
}
4.數(shù)據(jù)庫日志表
CREATE TABLE `sys_log_info` (
  `sl_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
  `module` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '模塊名稱',
  `type` varchar(10) COLLATE utf8_bin DEFAULT NULL COMMENT '類型',
  `title` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '標(biāo)題',
  `req_param` text COLLATE utf8_bin COMMENT '請求參數(shù)',
  `result` text COLLATE utf8_bin COMMENT '返回結(jié)果',
  `operator_id` int(11) DEFAULT NULL COMMENT '操作人id',
  `operator` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '操作人',
  `operator_time` datetime DEFAULT NULL COMMENT '操作時間',
  PRIMARY KEY (`sl_id`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
4.使用實例
/**
     * 新增
     */
    @ApiOperation(value="用于新增用戶")
    @RequestMapping(value="/user/add",method=RequestMethod.POST)
    @LoginRequire
    @SysLog(type="新增",name="新增用戶",module="用戶管理")
    public Result<Object> add(@RequestBody UserInfoAdd request){
        this.userInfoService.add(request);
        return new Result<>();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,262評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,922評論 25 709
  • mac 終端 常用命令基本命令1、列出文件ls 參數(shù) 目錄名 例: 看看驅(qū)動目錄下有什么:ls /S...
    Zhui_Do閱讀 652評論 0 5
  • 色彩不夠夢幻,黃色太深了
    清曉遠(yuǎn)閱讀 330評論 0 0

友情鏈接更多精彩內(nèi)容