设为首页 加入收藏

TOP

Spring AOP自定义注解方式实现日志管理(五)
2015-11-12 23:05:54 来源: 作者: 【 】 浏览:37
Tags:Spring AOP 定义 注解 方式 实现 日志 管理
ession中的用户?
? ? ? ? User user = (User) session.getAttribute(WebConstants.CURRENT_USER);?
? ? ? ? //获取请求ip?
? ? ? ? String ip = request.getRemoteAddr(); */
? ? ? ? //获取用户请求方法的参数并序列化为JSON格式字符串?
? ? ? ?
? ? ? ? User user = new User();
? ? ? ? user.setId(1);
? ? ? ? user.setName("张三");
? ? ? ? String ip = "127.0.0.1";
? ? ? ?
? ? ? ? String params = "";?
? ? ? ? if (joinPoint.getArgs() !=? null && joinPoint.getArgs().length > 0) {?
? ? ? ? ? ? for ( int i = 0; i < joinPoint.getArgs().length; i++) {?
? ? ? ? ? ? ? ? params += JsonUtil.getJsonStr(joinPoint.getArgs()[i]) + ";";?
? ? ? ? ? ? }?
? ? ? ? }?
? ? ? ? try {?
? ? ? ? ? ?
? ? ? ? ? ? String targetName = joinPoint.getTarget().getClass().getName();?
? ? ? ? ? ? String methodName = joinPoint.getSignature().getName();?
? ? ? ? ? ? Object[] arguments = joinPoint.getArgs();?
? ? ? ? ? ? Class targetClass = Class.forName(targetName);?
? ? ? ? ? ? Method[] methods = targetClass.getMethods();
? ? ? ? ? ? String operationType = "";
? ? ? ? ? ? String operationName = "";
? ? ? ? ? ? ? for (Method method : methods) {?
? ? ? ? ? ? ? ? ? if (method.getName().equals(methodName)) {?
? ? ? ? ? ? ? ? ? ? Class[] clazzs = method.getParameterTypes();?
? ? ? ? ? ? ? ? ? ? ? if (clazzs.length == arguments.length) {?
? ? ? ? ? ? ? ? ? ? ? ? ? operationType = method.getAnnotation(Log.class).operationType();
? ? ? ? ? ? ? ? ? ? ? ? ? operationName = method.getAnnotation(Log.class).operationName();
? ? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }
? ? ? ? ? ? /*========控制台输出=========*/?
? ? ? ? ? ? System.out.println("=====异常通知开始=====");?
? ? ? ? ? ? System.out.println("异常代码:" + e.getClass().getName());?
? ? ? ? ? ? System.out.println("异常信息:" + e.getMessage());?
? ? ? ? ? ? System.out.println("异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);?
? ? ? ? ? ? System.out.println("方法描述:" + operationName);?
? ? ? ? ? ? System.out.println("请求人:" + user.getName());?
? ? ? ? ? ? System.out.println("请求IP:" + ip);?
? ? ? ? ? ? System.out.println("请求参数:" + params);?
? ? ? ? ? ? ? /*==========数据库日志=========*/?
? ? ? ? ? ? SystemLog log = new SystemLog();
? ? ? ? ? ? log.setId(UUID.randomUUID().toString());
? ? ? ? ? ? log.setDescription(operationName);?
? ? ? ? ? ? log.setExceptioncode(e.getClass().getName());?
? ? ? ? ? ? log.setLogType((long)1);?
? ? ? ? ? ? log.setExceptionDetail(e.getMessage());?
? ? ? ? ? ? log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));?
? ? ? ? ? ? log.setParams(params);?
? ? ? ? ? ? log.setCreateBy(user.getName());?
? ? ? ? ? ? log.setCreateDate(new Date());?
? ? ? ? ? ? log.setRequestIp(ip);?
? ? ? ? ? ? //保存数据库?
? ? ? ? ? ? systemLogService.insert(log);?
? ? ? ? ? ? System.out.println("=====异常通知结束=====");?
? ? ? ? }? catch (Exception ex) {?
? ? ? ? ? ? //记录本地异常日志?
? ? ? ? ? ? logger.error("==异常通知异常==");?
? ? ? ? ? ? logger.error("异常信息:{}", ex.getMessage());?
? ? ? ? }?
? ? ? ? /*==========记录本地异常日志==========*/?
? ? ? ? logger.error("异常方法:{}异常代码:{}异常信息:{}参数:{}", joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(), e.getMessage(), params);?
?
? ? }?
? ?
}


我这里写的比较全,前置通知,环绕通知,后置通知,异常通知,后置饭后通知,都写上了,在我们实际编写中不写全也没事,我习惯上把记录日志的逻辑写在后置通知里面,我看网上也有些在前置通知里面的,但我感觉写在后置通知里比较好。


下面开始在controller中加入自定义的注解!!


package com.gcx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import com.gcx.annotation

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP的注解实例 下一篇Spring使用Cache

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: