自定义日志注解 + AOP实现记录操作日志
- 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实现记录操作日志的更多相关文章
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- spring-boot-route(十七)使用aop记录操作日志
在上一章内容中--使用logback管理日志,我们详细讲述了如何将日志生成文件进行存储.但是在实际开发中,使用文件存储日志用来快速查询问题并不是最方便的,一个优秀系统除了日志文件还需要将操作日志进行持 ...
- Appfuse:记录操作日志
appfuse的数据维护操作都发生在***form页面,与之对应的是***FormController,在Controller中处理数据的操作是onSubmit方法,既然所有的操作都通过onSubmi ...
- MVC 记录操作日志与过滤特殊字符
最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...
- Tomcat会话超时时怎样记录操作日志,满足安全审计要求
众所周知.在实际的Web应用程序中,会话管理一般都採用Web容器会话管理功能. 使用Tomcat做Webserver也是如此,并且从安全的角度考虑,尽量避免去更改和干预Web容器的会话管理功能. To ...
- Spring aop 记录操作日志 Aspect
前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...
- springmvc集成aop记录操作日志
首先说明一下,这篇文章只做了记录日志相关事宜 具体springmvc如何集成配置aop对cotroller进行拦截,请看作者的另一篇文章 http://www.cnblogs.com/guokai87 ...
- SpringBoot-AOP记录操作日志
package com.meeno.inner.oa.extend.operaterecord.aop; import com.alibaba.fastjson.JSONArray; import c ...
- Django记录操作日志、LogEntry的使用
LogEntry是在后台开发中经常用到的模块,它在admin是默认开启的. 可以使用LogEntry模块记录所有用户的操作记录.一方面可以用来监督,另一方面可以用来做回滚. 1. 使用LogEntry ...
随机推荐
- 用流的方式来操作hdfs上的文件
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- C printf格式化输出 的跨平台
printf()在不同的系统上,占位符的可能有不同的写法,如: %ld:long int, 32位平台4 bytes %lld: long long int, 32位平台8 bytes 可以考虑使用 ...
- ClientDataSet初步使用
https://blog.csdn.net/onebigday/article/details/5602619 ClientDataSet初步使用 2010年05月18日 08:36:00 阅读数:5 ...
- windows10安装ipython
Win10中如何装IPython?(其他Windows版本,如win7.win8/8.1也通用)我的这个方法比较简单,配置好环境变量敲几行命令就行了 .安装IPython的前提是已经安装好了Pytho ...
- CentOS 下 redis 安装与配置
CentOS 下 redis 安装与配置 1.到官网上找到合适版本下载解压安装 [root@java src]# wget -c http://redis.googlecode.com/files ...
- pyhton常用快捷键
常用快捷键 快捷键 功能 Ctrl + Q 快速查看文档 Ctrl + F1 显示错误描述或警告信息 Ctrl + / 行注释(可选中多行) Ctrl + Alt + L 代码格式化 Ctrl + A ...
- 简单实现一个textarea自适应高度
textarea自适应的实现方法很多,原理其实比较简单:监听textarea的input或者键盘事件,获取元素的scrollHeight,重置textarea元素的高度. 预览地址:textarea ...
- httplib模块:(一个相对底层的http请求模块)
httplib是一个相对底层的http请求模块,期上有专门的包装模块,如urllib内建模块,goto第三方模块,但是封装的越高就约不灵活,比如urllib模块里的请求错误是就不会返回结果页的内容,只 ...
- 定时器,定时发邮件JavaMail
一.定时器用法: 1.1先导入jar包 <!--spring整合其他文件时要用的jar包--> <dependency> <groupId>org.springfr ...
- 13DBUtils工具类
如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils. DBUtils就是JDBC的简化开发工具包.需要项 ...