服务器报错邮件发送错误日志

    类: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+"<br />"+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("<br /> USERID["+ SecurityUtils.getCurrentUserId()+"]");
        info.append("<br />ClassName=["+beanName+"."+methodName+"()]");
        info.append("<br />Params=["+params+"]");
        info.append("<br />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("<br />Url=["+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+uri+"("+method+")]");
            info.append("<br />RemoteAddr=["+remoteAddr+"]<br />");
        }
        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<toMailPaths.length;i++ ) {
                String toMailPath = toMailPaths[i];
                final MimeMessage mimeMessage = mailSender.createMimeMessage();//JavaMailSender
                final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                message.setFrom(jHipsterProperties.getMail().getFrom());
                message.setTo(toMailPath);
                message.setSubject(mailSubject);
                message.setText(webServerAttribute + mailContent, true);
                mailSender.send(mimeMessage);
            }
        }catch(Exception e){
            Logback.error(e, logger);
        }

    }

猜你喜欢

转载自my.oschina.net/zhangshsURL/blog/1788419