Spring AOP记录系统日志

配置文件:

Xml代码 
  1. <!-- 操作日志切面声明  -->     
  2.    <bean id="logAspect" class="com.tq365.service.sys.log.SystemLogAspect"/>  
  3.    <aop:config>  
  4.        <aop:aspect ref="logAspect">  
  5.        </aop:aspect>  
  6.    </aop:config>  

 

 

实现代码:

Java代码 
  1. /** 
  2.  * 系统操作日志切面 
  3.  *  
  4.  * @author archie2010 
  5.  *  since 2011-3-17 下午02:44:03 
  6.  */  
  7. @Aspect  
  8. public class SystemLogAspect {  
  9.   
  10.     // int与long之Class会自动转为其封装类型之Class  
  11.     private static final String integerClazz = "class java.lang.Integer";  
  12.     private static final String longClazz = "class java.lang.Long";  
  13.   
  14.     @Resource  
  15.     private SystemLogService systemLogService;  
  16.   
  17.     private Logger logger = Logger.getLogger(this.getClass().getName());  
  18.   
  19.     @Pointcut("execution(* com.tq365.service..*.*(..))")  
  20.     public void myAspect() {  
  21.     };  
  22.   
  23.     @AfterThrowing(pointcut = "myAspect()", throwing = "e")  
  24.     public void doAfterThrowing(JoinPoint jp, Throwable e) {  
  25.         System.out.println("出现异常:" + e.getMessage());  
  26.         System.out.println(e.getClass().getName());  
  27.         System.out.println("异常所在类:" + jp.getTarget().getClass().getName());  
  28.         System.out.println("" + jp.getSignature().getName()  
  29.                 + "方法 throw exception");  
  30.         // logger.error("错误! error级别的!!!"+e.getMessage());  
  31.         logger.error("Oops===" + jp.getTarget().getClass().getName() + "中的"  
  32.                 + jp.getSignature().getName() + "方法抛出" + e.getClass().getName()  
  33.                 + "异常");  
  34.         System.out.println("参数:");  
  35.         ;  
  36.         if (jp.getArgs() != null && jp.getArgs().length > 0) {  
  37.             for (int i = 0; i < jp.getArgs().length; i++) {  
  38.                 System.out.println(jp.getArgs()[i].toString());  
  39.                 logger.error("参数:--" + jp.getArgs()[i].toString());  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44.     @SuppressWarnings("unchecked")  
  45.     @After("@annotation(com.tq365.sys.annotation.SystemLogAnnotation)")  
  46.     public void doAfter(JoinPoint jp) {  
  47.         System.out.println("----------后置通知");  
  48.         System.out.println("方法所在类:" + jp.getTarget().getClass().getName());  
  49.         System.out.println("" + jp.getSignature().getName() + "方法");  
  50.   
  51.         String methodName = jp.getSignature().getName();  
  52.   
  53.         // 操作日志对象-----------------  
  54.         SystemLog sysLog = new SystemLog();  
  55.   
  56.         // 操作参数-----------------  
  57.         String descArgs = "参数";  
  58.         if (jp.getArgs() != null && jp.getArgs().length > 0) {  
  59.             for (int i = 0; i < jp.getArgs().length; i++) {  
  60.                 if(jp.getArgs()[i]!=null){  
  61.                     //System.out.println(jp.getArgs()[i].toString());  
  62.                     descArgs += jp.getArgs()[i].toString()+",";  
  63.                 }else{  
  64.                     descArgs +="null"+",";  
  65.                 }  
  66.             }  
  67.             System.out.println("------参数" + descArgs);  
  68.         }  
  69.         sysLog.setOperateArgs(descArgs);  
  70.   
  71.         String des = null;//方法描述  
  72.         if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {  
  73.   
  74.             Class targetClass = jp.getTarget().getClass();  
  75.   
  76.             //方法不定向参数Clazz...  
  77.             Class[] claszs = new Class[jp.getArgs().length];  
  78.   
  79.             for (int i = 0; i < jp.getArgs().length; i++) {  
  80.                 //System.out.println(jp.getArgs()[i]);  
  81.                 if(jp.getArgs()[i]!=null){  
  82.                     System.out.println(jp.getArgs()[i].getClass());  
  83.                     if (jp.getArgs()[i].getClass().toString().equals(integerClazz)) {  
  84.                         claszs[i] = int.class;  
  85.                     } else if (jp.getArgs()[i].getClass().toString().equals(  
  86.                             longClazz)) {  
  87.                         claszs[i] = long.class;  
  88.                     }else{  
  89.                         claszs[i] =jp.getArgs()[i].getClass();  
  90.                     }  
  91.                 }else if(jp.getArgs()[i]==null){  
  92.                     claszs[i] = String.class;  
  93.                 }  
  94.             }  
  95.             Method method=null;  
  96.             try {  
  97.                 method = targetClass.getMethod(methodName, claszs);  
  98.             } catch (SecurityException e) {  
  99.             } catch (NoSuchMethodException e) {  
  100.             }  
  101.             //若方法为空(描述无法获得则des=null)  
  102.             if(method!=null){  
  103.                 System.out.println(method.getAnnotation(SystemLogAnnotation.class)  
  104.                         .description());  
  105.                 des = method.getAnnotation(SystemLogAnnotation.class).description();  
  106.             }  
  107.   
  108.         }  
  109.         // 获得Session  
  110.         HttpSession session = ServletActionContext.getRequest().getSession();  
  111.         // 取到当前的操作用户  
  112.         User appUser = (User) session.getAttribute("USER");  
  113.   
  114.         if (appUser != null) {  
  115.             System.out.println("用户已经存在Session中");  
  116.             // 操作日志对象  
  117.   
  118.             sysLog.setUid(appUser.getUserId());  
  119.             sysLog.setUsername(appUser.getFullName());  
  120.   
  121.         }  
  122.         HttpServletRequest request = ServletActionContext.getRequest();  
  123.         String ip = request.getRemoteAddr();  
  124.         sysLog.setOperateTime(DateUtil.getCurrentTime());  
  125.         sysLog.setOperateDes(methodName +"->"+ des);  
  126.         sysLog.setIp(ip);  
  127.   
  128.         systemLogService.save(sysLog);  
  129.         System.out.println("----------保存操作日志");  
  130.     }  
  131.   
  132. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值