消息日誌類-spring aop
阿新 • • 發佈:2017-09-16
num tps cat put servle private eof row ref
package com.apt.aop; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.alibaba.fastjson.JSONObject; import com.apt.dao.log.LogInfoRepository; import com.apt.model.log.LogInfo; import com.apt.util.ChkUtil; import com.apt.util.date.DateUtil; @Component @Aspectpublic class ExceptionLogHandler { private final static Logger logger = LoggerFactory.getLogger(ExceptionLogHandler.class); private final static String PROJECT_NAME = "aaa"; @Resource private LogInfoRepository logInfoRepository; @Pointcut("execution(* com.apt.web..*.*(..))") public void exceptionLog() { } @Around("exceptionLog()") public Object around(JoinPoint joinPoint) { LogInfo logInfo = new LogInfo(); logInfo.setProjectName(PROJECT_NAME); //Map<String,Object> map = new HashMap<String,Object>(); JSONObject json = new JSONObject(); Object reuslt = null; try { String targetName = joinPoint.getTarget().getClass().getName(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; String methodName = methodSignature.getName(); String[] params = methodSignature.getParameterNames(); Object[] arguments = joinPoint.getArgs(); for(int i=0; i<params.length; i++) { if(arguments[i] instanceof HttpServletRequest || arguments[i] instanceof HttpServletResponse) continue; json.put(params[i], arguments[i]); } logInfo.setLogClass(targetName); logInfo.setLogMethod(methodName); RequestAttributes ra = RequestContextHolder.getRequestAttributes(); String ip = ""; if(ra != null) { ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); /*String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString();*/ ip = request.getRemoteAddr(); logInfo.setRequestIp(ip); logInfo.setRequestUrl(url); } /*ClassPool pool = ClassPool.getDefault(); ClassClassPath classPath = new ClassClassPath(joinPoint.getTarget().getClass()); pool.insertClassPath(classPath); CtClass cc = pool.get(targetName); CtMethod cm = cc.getDeclaredMethod(methodName); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null) { // exception } String[] paramNames = new String[cm.getParameterTypes().length]; int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < paramNames.length; i++) { paramNames[i] = attr.variableName(i + pos); } for (int i = 0; i < paramNames.length; i++) { if(arguments[i] instanceof HttpServletRequest || arguments[i] instanceof HttpServletResponse) continue; map.put(paramNames[i], arguments[i]); } */ logInfo.setRequestParam(json.toString()); } catch(Exception e) { logger.info(e.getMessage(),e); logInfo.setErrorMsg(e.getMessage()); e.fillInStackTrace(); } try{ ProceedingJoinPoint point = (ProceedingJoinPoint) joinPoint; reuslt = point.proceed(); if(!ChkUtil.isEmpty(reuslt)) { logInfo.setResponseResult(reuslt.toString()); } } catch (Throwable e) { String msg = errorClassInfo(e); logInfo.setErrorMsg(msg+e.fillInStackTrace().getMessage()); } try{ logInfo.setCreateTime(DateUtil.getCurrentTime()); logInfoRepository.save(logInfo); }catch (Exception e) { logger.info(e.getMessage(),e); } return reuslt; } private String errorClassInfo(Throwable ex) { StackTraceElement[] stackTraceElementArr= ex.getStackTrace(); StackTraceElement stackTraceElement = stackTraceElementArr[0]; String msg = "Erro File:" + stackTraceElement.getFileName(); msg += "Erro Method:" + stackTraceElement.getMethodName(); msg += "Erro Line:" + stackTraceElement.getLineNumber(); return msg; } }
消息日誌類-spring aop