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

本文介绍了一个针对方法抛出异常的日志处理方案,并详细解释了如何通过邮件服务发送异常日志报告。该方案使用AOP技术实现,能够有效地捕获并记录异常信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    类: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);
        }

    }

转载于:https://my.oschina.net/zhangshsURL/blog/1788419

Python邮件450报错通常指的是在使用Python发送邮件时,遇到了一个SMTP错误,导致邮件无法成功发送。这个错误通常是由于以下几个原因造成的: 1. 目标邮件服务器暂时不可用或无法响应。 2. 发送方的邮件服务器没有被目标邮件服务器接受,可能是因为IP地址在黑名单上或者域名有不良记录。 3. 要发送邮件格式或内容不符合目标邮件服务器的要求。 4. 在SMTP会话中,由于认证失败或者其他原因导致的暂时性拒绝。 解决这个问题的一般步骤包括: - 确认目标邮件服务器运行正常,并且没有因为维护或其他原因暂时关闭。 - 检查邮件发送方的IP是否在黑名单中,以及域名的MX记录是否配置正确。 - 检查邮件内容是否包含被过滤的关键词或者有其他违规内容。 - 如果使用的是第三方邮件服务商,确认你的认证信息(如用户名和密码)是否正确无误。 - 查看邮件服务器提供的详细错误日志,以获取更精确的错误信息。 使用Python发送邮件通常会用到`smtplib`库,一个基本的发送邮件的代码示例如下: ```python import smtplib from email.mime.text import MIMEText from email.header import Header # 邮件服务器相关信息 smtp_server = 'smtp.example.com' smtp_port = 587 username = 'your_email@example.com' password = 'your_password' # 邮件发送者和接收者 sender = 'your_email@example.com' receiver = ['receiver_email@example.com'] # 邮件内容设置 message = MIMEText('This is the body of the email', 'plain', 'utf-8') message['From'] = Header("Your Name", 'utf-8') message['To'] = Header("Receiver's Name", 'utf-8') subject = 'Python SMTP Email Test' message['Subject'] = subject try: # 创建SMTP对象 server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # 启用安全传输模式 server.login(username, password) # 登录验证 server.sendmail(sender, receiver, message.as_string()) print("Email sent successfully") except Exception as e: print("Error: ", e) finally: server.quit() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值