类:ResourceLoggingAspect /** * 对于方法抛出异常后的操作 * BaseAppException的 400和200不会打印日志文件 * @param exception */ @AfterThrowing(pointcut = "resourceLogging()",throwing = "exception") public void afterThrowing(JoinPoint joinPoint,Exception exception) { try{ if(exception instanceof BaseAppException){ BaseAppException baseAppException=(BaseAppException) exception; if(baseAppException.getErrorCode() == null ||baseAppException.getErrorCode().equals(BaseDTO.CODE_PARAM) ||baseAppException.getErrorCode().equals(BaseDTO.CODE_SUCCESS)){ return; } }else{ String msg = joinPointToMsgForHtml(joinPoint); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); exception.printStackTrace(printWriter); sendMailService.sendErrorLogMail(msg+""+stringWriter.toString()); } String msg = joinPointToMsg(joinPoint); Logback.error( msg, exception, logger); }catch(Exception e){ Logback.error("操作错误日志(ERROR)记录失败[com.公司.项目名.gateway.aop.webLog.WebRequestLogAspect.afterThrowing()]", e,logger); } } /** * 根据切点获取请求,返回打印信息 */ private String joinPointToMsgForHtml(JoinPoint joinPoint) { String beanName = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); Object[] paramsArray = joinPoint.getArgs(); String params = argsArrayToString(paramsArray); StringBuffer info=new StringBuffer(); info.append(" USERID["+ SecurityUtils.getCurrentUserId()+"]"); info.append("ClassName=["+beanName+"."+methodName+"()]"); info.append("Params=["+params+"]"); info.append("CurrentDate=["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date())+"]"); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(attributes != null){ HttpServletRequest request = attributes.getRequest(); String uri = request.getRequestURI(); String remoteAddr = getIpAddr(request); String method = request.getMethod(); info.append("Url=["+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+uri+"("+method+")]"); info.append("RemoteAddr=["+remoteAddr+"]"); } return info.toString(); } /** * 根据切点获取请求,返回打印信息 */ private String joinPointToMsg(JoinPoint joinPoint) { // 接收到请求,记录请求内容 String beanName = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); Object[] paramsArray = joinPoint.getArgs(); String params = argsArrayToString(paramsArray); StringBuffer info=new StringBuffer(); info.append("\nUSERID["+ SecurityUtils.getCurrentUserId()+"]"); info.append("\nClassName=["+beanName+"."+methodName+"()]"); info.append("\nParams=["+params+"]"); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(attributes != null){ HttpServletRequest request = attributes.getRequest(); String uri = request.getRequestURI(); String remoteAddr = getIpAddr(request); String method = request.getMethod(); info.append("\nUrl=["+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+uri+"("+method+")]"); info.append("\nRemoteAddr=["+remoteAddr+"]"); } return info.toString(); } 类:SendMailService /** * 发送邮件 * @param mailContent 邮件内容 */ public void sendErrorLogMail(String mailContent) { try{ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd_HHmmss"); String timeStamp = simpleDateFormat.format(new Date()); final String mailSubject = "[Process"+timeStamp+"]ERROR日志报告"; String webServerAttribute = WebServerAttributeUtil.attributeToStringForHtml(webServerUrl); for(int i=0; i