由于时间宝贵,有时候写博客就没写那么详细,有什么地方不明白的话可以通过微信联系到我,我在我的某一篇文章中留下过自己的微信二维码,欢迎来骚扰....
系统发生异常时,邮件提醒
通过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容器才会扫描到)
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;
}
}