spring配置日志切面,实现系统操作日志记录
来源:互联网 发布:java如何调用数组 编辑:程序博客网 时间:2024/06/10 09:45
//做系统是经常会遇到的情况之一,对系统操作日志存表记录
下面给出下例子
需要注意的是,日志通常数据量会很大,建议已每个人月一张表,或者其他方式分表
例如:logs_2012_1
logs_2012_2
logs_2012_3
这样的形式。
需要引入的jar包
至少得有aop和切面包
1,日志切面类
import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.aspectj.lang.ProceedingJoinPoint;import com.opensymphony.xwork2.ActionContext;/** * 日志记录仪 */public class Logger {private LogService logService ;//注入logServicepublic void setLogService(LogService logService) {this.logService = logService;}/** * 记录日志 */public Object record(ProceedingJoinPoint pjp){Log log = new Log();try {ActionContext ac = ActionContext.getContext();//operatorif(ac != null){HttpServletRequest req = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);if(req != null){User user = (User) req.getSession().getAttribute("user");if(user != null){log.setOperator("" + user.getId() + ":" + user.getEmail());}}}//operName,方法名String methodName = pjp.getSignature().getName();log.setOperName(methodName);//operParams,方法参数列表Object[] args = pjp.getArgs();log.setOperParams(StringUtil.arr2Str(args));//调用目标对象的方法Object ret = pjp.proceed();//operResult,成功log.setOperResult("success");//resultMsg,结果消息if(ret != null){log.setResultMsg(ret.toString());}return ret ;} catch (Throwable e) {log.setOperResult("failure") ;log.setResultMsg(e.getMessage());}finally{logService.saveEntity(log);//保存日志的service方法}return null ;}}
2,spring配置文件中写法
<?xml version="1.0"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "><!-- logger 将日志切面类的完整路径配置进来--><bean id="logger" class="cn.itcast.surveypark.advice.Logger"> <!-- 注入切面中需要的资源--><property name="logService" ref="logService" /></bean><!-- aop事务配置 --><aop:config> <!-- 日志切入点 这里一定要注意,要把插入日志操作的service排除(and !bean(logService),不然后形成死循环,因为日志操作类本身也是在进行写操作 --><aop:pointcut expression="(execution(* *..*Service.save*(..))or execution(* *..*Service.update*(..))or execution(* *..*Service.delete*(..))or execution(* *..*Service.batch*(..))or execution(* *..*Service.new*(..))) and !bean(logService)"id="loggerPointcut" /><!-- 配置日志切面 配置order="1"是为了让日志切面最先执行--><aop:aspect id="loggerAspect" ref="logger" order="1"><aop:around method="record" pointcut-ref="loggerPointcut" /></aop:aspect></aop:config></beans>
0 0
- spring配置日志切面,实现系统操作日志记录
- Spring AOP 实现系统操作日志记录
- Spring AOP 实现系统操作日志记录
- Spring 切面 记录请求日志
- springmvc 切面记录操作日志
- springmvc 切面记录操作日志
- 使用Spring切面编程记录操作行为的日志
- Spring—AOP实践,利用切面记录用户操作日志
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录
- spring aop 切面记录log4j日志
- spring的AOP实现记录操作日志
- spring aop 实现操作日志记录
- Spring AOP实现系统日志记录
- Spring AOP实现系统日志记录
- spring aop 实现系统日志的记录
- Spring aop切面做日志处理操作
- 配置spring日志记录
- aop切面记录日志
- tinyxml解析文件遇到空值的解决方法
- Qt on Android: Android SDK安装
- 第一次到这论坛,今后坚持下去,努力成长。
- protobuf-overview
- 据说,年薪百万的程序员,都是这么开悟的
- spring配置日志切面,实现系统操作日志记录
- ExtJs对js基本语法扩展支持
- 【iOS7的一些总结】8、破解UIView中坐标系的秘密:关于UIView中的frame、bound和center
- openstack学习之白话Openstack
- hibernate环境搭建与例子
- Codeforces 433C Ryouko's Memory Note
- 离散数学 c++ 地图涂色
- android实现应用程序只有在第一次启动时显示引导界面
- 纯javascript实现“贪吃蛇”之01-----Day16