spring配置日志切面,实现系统操作日志记录

逃离我推掉我的手 2022-08-27 09:48 280阅读 0赞

//做系统是经常会遇到的情况之一,对系统操作日志存表记录

下面给出下例子

需要注意的是,日志通常数据量会很大,建议已每个人月一张表,或者其他方式分表

例如:logs_2012_1

  1. logs\_2012\_2
  2. logs\_2012\_3

这样的形式。

需要引入的jar包

至少得有aop和切面包

1,日志切面类

  1. import javax.servlet.http.HttpServletRequest;
  2. import org.apache.struts2.ServletActionContext;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import com.opensymphony.xwork2.ActionContext;
  5. /**
  6. * 日志记录仪
  7. */
  8. public class Logger {
  9. private LogService logService ;
  10. //注入logService
  11. public void setLogService(LogService logService) {
  12. this.logService = logService;
  13. }
  14. /**
  15. * 记录日志
  16. */
  17. public Object record(ProceedingJoinPoint pjp){
  18. Log log = new Log();
  19. try {
  20. ActionContext ac = ActionContext.getContext();
  21. //operator
  22. if(ac != null){
  23. HttpServletRequest req = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
  24. if(req != null){
  25. User user = (User) req.getSession().getAttribute("user");
  26. if(user != null){
  27. log.setOperator("" + user.getId() + ":" + user.getEmail());
  28. }
  29. }
  30. }
  31. //operName,方法名
  32. String methodName = pjp.getSignature().getName();
  33. log.setOperName(methodName);
  34. //operParams,方法参数列表
  35. Object[] args = pjp.getArgs();
  36. log.setOperParams(StringUtil.arr2Str(args));
  37. //调用目标对象的方法
  38. Object ret = pjp.proceed();
  39. //operResult,成功
  40. log.setOperResult("success");
  41. //resultMsg,结果消息
  42. if(ret != null){
  43. log.setResultMsg(ret.toString());
  44. }
  45. return ret ;
  46. } catch (Throwable e) {
  47. log.setOperResult("failure") ;
  48. log.setResultMsg(e.getMessage());
  49. }
  50. finally{
  51. logService.saveEntity(log);//保存日志的service方法
  52. }
  53. return null ;
  54. }
  55. }

2,spring配置文件中写法

  1. <?xml version="1.0"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:cache="http://www.springframework.org/schema/cache"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/cache
  15. http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
  16. <!-- logger 将日志切面类的完整路径配置进来-->
  17. <bean id="logger" class="cn.itcast.surveypark.advice.Logger">
  18. <!-- 注入切面中需要的资源-->
  19. <property name="logService" ref="logService" />
  20. </bean>
  21. <!-- aop事务配置 -->
  22. <aop:config>
  23. <!-- 日志切入点 这里一定要注意,要把插入日志操作的service排除(and !bean(logService),
  24. 不然后形成死循环,因为日志操作类本身也是在进行写操作 -->
  25. <aop:pointcut expression="(execution(* *..*Service.save*(..))
  26. or execution(* *..*Service.update*(..))
  27. or execution(* *..*Service.delete*(..))
  28. or execution(* *..*Service.batch*(..))
  29. or execution(* *..*Service.new*(..))) and !bean(logService)"id="loggerPointcut" />
  30. <!-- 配置日志切面 配置order="1"是为了让日志切面最先执行-->
  31. <aop:aspect id="loggerAspect" ref="logger" order="1">
  32. <aop:around method="record" pointcut-ref="loggerPointcut" />
  33. </aop:aspect>
  34. </aop:config>
  35. </beans>

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读