最近写项目考虑把一些运行时候出现的异常发送邮箱,这样可以随时监控错误,网上看可以用log4J发送错误日志到邮箱,现在把这种方式记录下来,以备下次用
1.log4j方式
需要jar包
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
<!–log4J–> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> log4j.properties配置 这里需要配置上MAIL
log4j.rootLogger =MAIL
#发送错误邮件 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=ERROR log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=*****@126.com log4j.appender.MAIL.SMTPHost=smtp.126.com log4j.appender.MAIL.SMTPUsername=***** log4j.appender.MAIL.SMTPPassword=******* log4j.appender.MAIL.SMTPDebug=true log4j.appender.MAIL.Subject=Log4JErrorMessage log4j.appender.MAIL.To=*****@163.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework]%d – %c -%-4r[%t]%-5p %c %x -%m%n</span></strong> 我用126邮箱发送给163邮箱,打码的依次是发送邮箱名,邮箱名,密码,接收邮箱名, SMTPUHost根据每个邮箱不同,需要自己修改,发送邮件的日志级别是ERROR型。
自己测试,每次会发送三次邮件,一次是自己做aop异常捕获的,还有两次是druid打印的错误日志,有点繁琐。决定用第二种方式。
2.自定义邮件发送util
这个还是需要上面的mail包,下面是我自定义的异常邮件发送工具
import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.UnsupportedEncodingException; import java.util.Properties; /** * Description * * @author wangsong * @date 19:39 2018/1/30 */ public class ErrorLogMail { private static final String HOST = "smtp.126.com"; private static final Integer PORT = 25; private static final String USERNAME = "***@126.com"; private static final String PASSWORD = "****"; private static final String EMAILFORM = "***@126.com"; private static JavaMailSenderImpl mailSender = createMailSender(); /** * 邮件发送器 * * @return 配置好的工具 */ private static JavaMailSenderImpl createMailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(HOST); sender.setPort(PORT); sender.setUsername(USERNAME); sender.setPassword(PASSWORD); sender.setDefaultEncoding("Utf-8"); Properties p = new Properties(); p.setProperty("mail.smtp.timeout", "25000"); p.setProperty("mail.smtp.auth", "false"); sender.setJavaMailProperties(p); return sender; } /** * 发送邮件 * @param html 发送内容 * @throws MessagingException 异常 * @throws UnsupportedEncodingException 异常 */ public static void sendHtmlMail( String html) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); // 设置utf-8或GBK编码,否则邮件会有乱码 MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(EMAILFORM, "wangsong"); messageHelper.setTo("******@163.com"); messageHelper.setSubject("ErrorLog"); messageHelper.setText(html, true); mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MailException e) { e.printStackTrace(); } }
logger.error(pjp.getSignature() " error ", e); //不可知异常,发送邮件ErrorLogMail.sendHtmlMail("时间:" new Date() "方法:" pjp.getSignature() "原因:" e.toString());
方法加在aop捕获异常处,完成更多关于云服务器,域名注册,虚拟主机的问题,请访问西部数码官网:www.west.cn