SpringBoot整合log4j发送系统异常邮件:实时掌控你的系统健康状态

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringBoot整合log4j发送系统异常邮件:实时掌控你的系统健康状态

Mr.Elliot 2018-09-05 18:25:00 浏览1788
展开阅读全文
img_885b38e7e94d832860d369caa2b1cd52.png
Exception Mail

由于时间宝贵,有时候写博客就没写那么详细,有什么地方不明白的话可以通过微信联系到我,我在我的某一篇文章中留下过自己的微信二维码,欢迎来骚扰....

系统发生异常时,邮件提醒


通过Log4j就可以实现这项功能,只需要简单配置一下

pom依赖去除

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--去除Springboot自带的日志包-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
log4j坐标依赖
<!--SpringBoot - log4j-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
<!--log4j 实现发邮件必须包-->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
javax.mail
<!--javax.mail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

log4j.properties(名字必须叫log4j.properties Spring容器才会扫描到)

img_93890d8c48985fcd32b17ea598c88cfd.png
log4j.properties
log4j.rootLogger=info,console,MAIL
log4j.logger.org.apache.mina.filter.logging.LoggingFilter=WARN
log4j.logger.com.playsnail.core.web.filter.IdentityFilter=WARN
log4j.logger.com.d1xn.common.log.Log=WARN
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%p-%d{yyyy/MM/dd/HH\:mm\:ss}]%c{1}.(%L) - %m%n
log4j.appender.console.Threshold=INFO

######################send error through email.######################
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#日志的错误级别
log4j.appender.MAIL.Threshold=ERROR
#缓存文件大小,日志达到2时发送Email
#log4j.appender.MAIL.BufferSize=2
#发送邮件的服务器
log4j.appender.MAIL.SMTPHost=smtp.126.com
#邮件主题
log4j.appender.MAIL.Subject=AwesomeBoy_LP Community Exception Mail
#发送邮件箱的用户
log4j.appender.MAIL.SMTPUsername=calistaangel@126.com
#发送邮件箱的密码
log4j.appender.MAIL.SMTPPassword=******
#发送邮件箱
log4j.appender.MAIL.From=******@126.com
#接受邮件箱
log4j.appender.MAIL.To=******@126.com
#发送邮件的格式
log4j.appender.MAIL.layout=org.apache.log4j.HTMLLayout

简易测试 :logger.error

/**
 * @Author:LiuPu
 * @Date:2018/9/5 17:11
 * @Description:
 * @Version 1.011
 */
public class Logtest {
    @Test
    public void logtest() {
        Logger logger = Logger.getLogger(Logtest.class);
        try {
            int i = 0;
            int b = 10;
            int c = b/i;
        } catch (Exception e) {
            // TODO: handle exception
            String currentTime = DateUtils.getCurrentTime12();
            logger.error("异常发生时间:"+currentTime,e);
        }
    }
}

DateUtils:

/**
 * @Author:LiuPu
 * @Date:2018/9/5 16:45
 * @Description:时间格式化工具
 * @Version 1.011
 */
public class DateUtils {
    public static String getCurrentTime24(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(new Date());
        return currentTime;
    }

    public static String getCurrentTime12EN(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss",Locale.ENGLISH);
        String currentTime = dateFormat.format(new Date());
        return currentTime;
    }

    public static String getCurrentTime12CHS(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss");
        String currentTime = dateFormat.format(new Date());
        return currentTime;
    }
}
img_0757ddd9bf0f454c282e0a0fdf9ae040.png
异常邮件

网友评论

登录后评论
0/500
评论