java切面获取异常日志_使用SpringBoot AOP 记录操作日志、异常日志

packagecom.hyd.zcar.cms.common.utils.aop;importjava.lang.reflect.Method;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importorg.springframework.web.context.request.RequestAttributes;importorg.springframework.web.context.request.RequestContextHolder;importcom.gexin.fastjson.JSON;importcom.hyd.zcar.cms.common.utils.IPUtil;importcom.hyd.zcar.cms.common.utils.annotation.OperLog;importcom.hyd.zcar.cms.common.utils.base.UuidUtil;importcom.hyd.zcar.cms.common.utils.security.UserShiroUtil;importcom.hyd.zcar.cms.entity.system.log.ExceptionLog;importcom.hyd.zcar.cms.entity.system.log.OperationLog;importcom.hyd.zcar.cms.service.system.log.ExceptionLogService;importcom.hyd.zcar.cms.service.system.log.OperationLogService;/*** 切面处理类,操作日志异常日志记录处理

*

*@authorwu

* @date 2019/03/21*/@Aspect

@Componentpublic classOperLogAspect {/*** 操作版本号

*

* 项目启动时从命令行传入,例如:java -jar xxx.war --version=201902

*

*/@Value("${version}")privateString operVer;

@AutowiredprivateOperationLogService operationLogService;

@AutowiredprivateExceptionLogService exceptionLogService;/*** 设置操作日志切入点 记录操作日志 在注解的位置切入代码*/@Pointcut("@annotation(com.hyd.zcar.cms.common.utils.annotation.OperLog)")public voidoperLogPoinCut() {

}/*** 设置操作异常切入点记录异常日志 扫描所有controller包下操作*/@Pointcut("execution(* com.hyd.zcar.cms.controller..*.*(..))")public voidoperExceptionLogPoinCut() {

}/*** 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行

*

*@paramjoinPoint 切入点

*@paramkeys 返回结果*/@AfterReturning(value= "operLogPoinCut()", returning = "keys")public voidsaveOperLog(JoinPoint joinPoint, Object keys) {//获取RequestAttributes

RequestAttributes requestAttributes =RequestContextHolder.getRequestAttributes();//从获取RequestAttributes中获取HttpServletRequest的信息

HttpServletRequest request =(HttpServletRequest) requestAttributes

.resolveReference(RequestAttributes.REFERENCE_REQUEST);

OperationLog operlog= newOperationLog();try{

operlog.setOperId(UuidUtil.get32UUID());//主键ID//从切面织入点处通过反射机制获取织入点处的方法

MethodSignature signature =(MethodSignature) joinPoint.getSignature();//获取切入点所在的方法

Method method =signature.getMethod();//获取操作

OperLog opLog = method.getAnnotation(OperLog.class);if (opLog != null) {

String operModul=opLog.operModul();

String operType=opLog.operType();

String operDesc=opLog.operDesc();

operlog.setOperModul(operModul);//操作模块

operlog.setOperType(operType); //操作类型

operlog.setOperDesc(operDesc); //操作描述

}//获取请求的类名

String className =joinPoint.getTarget().getClass().getName();//获取请求的方法名

String methodName =method.getName();

methodName= className + "." +methodName;

operlog.setOperMethod(methodName);//请求方法//请求的参数

Map rtnMap =converMap(request.getParameterMap());//将参数所在的数组转换成json

String params =JSON.toJSONString(rtnMap);

operlog.setOperRequParam(params);//请求参数

operlog.setOperRespParam(JSON.toJSONString(keys)); //返回结果

operlog.setOperUserId(UserShiroUtil.getCurrentUserLoginName()); //请求用户ID

operlog.setOperUserName(UserShiroUtil.getCurrentUserName()); //请求用户名称

operlog.setOperIp(IPUtil.getRemortIP(request)); //请求IP

operlog.setOperUri(request.getRequestURI()); //请求URI

operlog.setOperCreateTime(new Date()); //创建时间

operlog.setOperVer(operVer); //操作版本

operationLogService.insert(operlog);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 异常返回通知,用于拦截异常日志信息 连接点抛出异常后执行

*

*@paramjoinPoint 切入点

*@parame 异常信息*/@AfterThrowing(pointcut= "operExceptionLogPoinCut()", throwing = "e")public voidsaveExceptionLog(JoinPoint joinPoint, Throwable e) {//获取RequestAttributes

RequestAttributes requestAttributes =RequestContextHolder.getRequestAttributes();//从获取RequestAttributes中获取HttpServletRequest的信息

HttpServletRequest request =(HttpServletRequest) requestAttributes

.resolveReference(RequestAttributes.REFERENCE_REQUEST);

ExceptionLog excepLog= newExceptionLog();try{//从切面织入点处通过反射机制获取织入点处的方法

MethodSignature signature =(MethodSignature) joinPoint.getSignature();//获取切入点所在的方法

Method method =signature.getMethod();

excepLog.setExcId(UuidUtil.get32UUID());//获取请求的类名

String className =joinPoint.getTarget().getClass().getName();//获取请求的方法名

String methodName =method.getName();

methodName= className + "." +methodName;//请求的参数

Map rtnMap =converMap(request.getParameterMap());//将参数所在的数组转换成json

String params =JSON.toJSONString(rtnMap);

excepLog.setExcRequParam(params);//请求参数

excepLog.setOperMethod(methodName); //请求方法名

excepLog.setExcName(e.getClass().getName()); //异常名称

excepLog.setExcMessage(stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace())); //异常信息

excepLog.setOperUserId(UserShiroUtil.getCurrentUserLoginName()); //操作员ID

excepLog.setOperUserName(UserShiroUtil.getCurrentUserName()); //操作员名称

excepLog.setOperUri(request.getRequestURI()); //操作URI

excepLog.setOperIp(IPUtil.getRemortIP(request)); //操作员IP

excepLog.setOperVer(operVer); //操作版本号

excepLog.setOperCreateTime(new Date()); //发生异常时间

exceptionLogService.insert(excepLog);

}catch(Exception e2) {

e2.printStackTrace();

}

}/*** 转换request 请求参数

*

*@paramparamMap request获取的参数数组*/

public Map converMap(MapparamMap) {

Map rtnMap = new HashMap();for(String key : paramMap.keySet()) {

rtnMap.put(key, paramMap.get(key)[0]);

}returnrtnMap;

}/*** 转换异常信息为字符串

*

*@paramexceptionName 异常名称

*@paramexceptionMessage 异常信息

*@paramelements 堆栈信息*/

publicString stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {

StringBuffer strbuff= newStringBuffer();for(StackTraceElement stet : elements) {

strbuff.append(stet+ "\n");

}

String message= exceptionName + ":" + exceptionMessage + "\n\t" +strbuff.toString();returnmessage;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值