需求:系统中经常需要记录员工的操作日志和用户的活动日志,简单的做法在每个需要的方法中进行日志保存操作,
但这样对业务代码入侵性太大,下面就结合AOP和自定义日志注解实现更方便的日志记录
 
首先看下一个简单的操作日志表
 
action_log
  • id
  • subject(日志主题)
  • content(日志内容)
  • create_by
  • create_time

日志主题可以用下面的枚举类来实现

package cn.bounter.common.model;

/**
* 应用日志主题枚举类
* @author simon
*
*/
public enum AppLogSubjectEnum { /** 客户 */
CUSTOMER(1,"客户"),
/** 商品 */
COMMODITY(2,"商品"),
/** 订单 */
ORDER(3,"订单"); private Integer value; private String name; private AppLogSubjectEnum(int value, String name) {
this.value = value;
this.name = name;
} public Integer getValue() {
return value;
} public String getName() {
return name;
} /**
* 自定义方法
* 根据枚举值获取枚举字符串内容
* @param value
* @return
*/
public static String stringOf(int value) {
for(AppLogSubjectEnum oneEnum : AppLogSubjectEnum.values()) {
if(oneEnum.value == value) {
return oneEnum.getName();
}
}
return null;
} }

然后让我们看下自定义注解

package cn.bounter.common.model;

import java.lang.annotation.*;

/**
* 应用日志注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AppLog { /**
* 日志主题
* @return
*/
AppLogSubjectEnum subject(); /**
* 日志内容
* @return
*/
String content() default "";
}

接下来就是重头戏基于自定义注解的切面了

package cn.bounter.common.model;

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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import java.lang.reflect.Method;
import java.time.LocalDateTime; /**
* 应用日志切面
*/
@Aspect
@Component
public class AppLogAspect { @Autowired
private ActionLogService actionLogService; @Pointcut("@annotation(cn.bounter.common.model.AppLog)")
public void appLogPointCut() {
} @AfterReturning("appLogPointCut()")
public void addActionLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod(); //获取注解
AppLog appLog = method.getAnnotation(AppLog.class);
if (appLog == null) {
return;
} //保存数据库
actionLogService.save(
new ActionLog()
.setSubject(appLog.subject().getValue())
.setContent(appLog.content())
.setCreateBy(ShiroUtils.getUser()) //获取当前登录的用户
.setCreateTime(LocalDateTime.now())
);
} }

到这里就差不多,最后让我们看下怎么使用自定义日志注解

/**
* 新增订单
* @param order
* @return
*/
@AppLog(subject = AppLogSubjectEnum.ORDER, content = "新增")
public void save(Order order) {
orderService.save(order);
}

看完了之后是不是觉得挺简单哉!那就赶快自己动手试一试吧!

自定义日志注解 + AOP实现记录操作日志的更多相关文章

  1. 使用SpringBoot AOP 记录操作日志、异常日志

    平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...

  2. spring-boot-route(十七)使用aop记录操作日志

    在上一章内容中--使用logback管理日志,我们详细讲述了如何将日志生成文件进行存储.但是在实际开发中,使用文件存储日志用来快速查询问题并不是最方便的,一个优秀系统除了日志文件还需要将操作日志进行持 ...

  3. Appfuse:记录操作日志

    appfuse的数据维护操作都发生在***form页面,与之对应的是***FormController,在Controller中处理数据的操作是onSubmit方法,既然所有的操作都通过onSubmi ...

  4. MVC 记录操作日志与过滤特殊字符

    最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...

  5. Tomcat会话超时时怎样记录操作日志,满足安全审计要求

    众所周知.在实际的Web应用程序中,会话管理一般都採用Web容器会话管理功能. 使用Tomcat做Webserver也是如此,并且从安全的角度考虑,尽量避免去更改和干预Web容器的会话管理功能. To ...

  6. Spring aop 记录操作日志 Aspect

    前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...

  7. springmvc集成aop记录操作日志

    首先说明一下,这篇文章只做了记录日志相关事宜 具体springmvc如何集成配置aop对cotroller进行拦截,请看作者的另一篇文章 http://www.cnblogs.com/guokai87 ...

  8. SpringBoot-AOP记录操作日志

    package com.meeno.inner.oa.extend.operaterecord.aop; import com.alibaba.fastjson.JSONArray; import c ...

  9. Django记录操作日志、LogEntry的使用

    LogEntry是在后台开发中经常用到的模块,它在admin是默认开启的. 可以使用LogEntry模块记录所有用户的操作记录.一方面可以用来监督,另一方面可以用来做回滚. 1. 使用LogEntry ...

随机推荐

  1. Firefox,Chrome使用

    Firefox 插件 REDIRECTOR Automatically redirect pages based on user-defined rules. 根据用户定义的规则自动重定向页面的插件. ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_2_一切皆为字节

    这里的视频就是字节的形式,为了看着方便转换成了MB.一个字节就是8个二进制 包括文本,都是以字节的形式存储的

  3. PMBOK

    项目章程的内容1. 基于项目干系人的需求和期望提出的要求.2. 项目必须满足的业务要求或产品需求.3. 项目的目的或项目立项的理由.4. 委派的项目经理及项目经理的权限级别.5. 概要的里程碑进度计划 ...

  4. 【Linux】limits.conf 不重启就生效或者不生效的原因

    前阵子,我要用到使LInux的文件打开数为65534个,而且需要永久生效,于是将配置写到了: vim /etc/security/limits.conf * soft nofile 65534* ha ...

  5. spring Cloud 之 Eureka、Feign、Hystrix、Zuul、Config、Bus

    一.服务发现——Netflix Eureka Eureka包含两个组件: Eureka Server和Eureka Client 1.创建Eureka Server服务端 (1).引入依赖 父工程po ...

  6. oracle--二维表的操作创建修改删除

    oracle学习内容 oracle的管理系统学习 oracle的数据管理学习 oracle的用户管理 oracle二维表管理 创建表和字段讲解 --创建表学习 1. 创建表的基本语句:create t ...

  7. python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  8. QT 5.12安装

    QT 5.12为最新的LTS版本,将通过该版本开始QT的学习 1.软件下载 QT5.12下载地址:http://download.qt.io/archive/qt/5.12/ 当前最新版本为5.12. ...

  9. 3486 ( Interviewe )RMQ

    Problem Description YaoYao has a company and he wants to employ m people recently. Since his company ...

  10. sweetalert插件替代alert/confirm

    更多关于SweetAlert的内容请参考:https://github.com/t4t5/sweetalert.