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