07.03 架構實戰篇(十三):Spring Boot Logback 郵件通知

架構實戰篇(十三):Spring Boot Logback 郵件通知

日誌對於應用程序來說是非常重要的,當你的程序報錯了,而你又不知道是多麼可怕的一件事情,本文使用logback把程序報錯信息郵件到開發者

目錄結構

架構實戰篇(十三):Spring Boot Logback 郵件通知

目錄結構

項目依賴配置

pom.xml


<project> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<groupid>com.itunion/<groupid>
<artifactid>spring-boot-log-mail/<artifactid>
<version>0.0.1-SNAPSHOT/<version>
<packaging>jar/<packaging>
<name>spring-boot-log-mail/<name>
<description>Demo project for Spring Boot/<description>
<parent>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-parent/<artifactid>
<version>2.0.3.RELEASE/<version>
<relativepath>
/<parent>
<properties>
<project.build.sourceencoding>UTF-8/<project.build.sourceencoding>
<project.reporting.outputencoding>UTF-8/<project.reporting.outputencoding>
<java.version>1.8/<java.version>
/<properties>
<dependencies>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-devtools/<artifactid>
<scope>runtime/<scope>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-test/<artifactid>
<scope>test/<scope>
/<dependency>

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-mail/<artifactid>
/<dependency>
/<dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<plugins>
/<build>
/<project>

這裡主要使用了spring boot 郵件的依賴 spring-boot-starter-mail , 確實好方便

日誌郵件配置

mail-appender.xml


<included>

<property>
<property>
<property>
<property>
<property>
<property>
<property>
<property>
<appender>
<smtphost>${smtpHost}/<smtphost>
<smtpport>${smtpPort}/<smtpport>
<username>${username}/<username>
<password>${password}/<password>
${SSL}
<asynchronoussending>false/<asynchronoussending>
${mailTo}

<from>${mailFrom}/<from>
<subject>${mailSubject}/<subject>
<layout>
<filter>
<level>ERROR/<level>
/<filter>
/<appender>
/<included>
<property>
<property>
<property>
<property>

這裡需要把你自己的郵箱配置上去,並填上你的授權密碼

郵箱記得打開 smtp 設置

郵箱記得打開 smtp 設置

郵箱記得打開 smtp 設置

Logback 配置

logback.xml


<configuration>

<include>

<property>

<include>

<include>

<include>
<root>
<appender-ref>
<appender-ref>
<appender-ref>
/<root>
/<configuration>

這裡導入郵件配置文件 和增加一行

日誌文件配置

application.properties

logging.path=../logs
logging.file=../logs/file.log
logging.level.root=info
spring.http.encoding.charset=utf-8

模擬異常

AppController

package com.itunion.log.mail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RequestMapping("/app")
@RestController
public class AppController {
private Logger logger = LoggerFactory.getLogger(AppController.class);
@RequestMapping
public String selectList() {
boolean flag = new Random().nextBoolean();
if (flag) {

throw new RuntimeException("查詢報錯");
} else {
return "正常返回 當前時間:" + System.currentTimeMillis();
}
}
@ExceptionHandler
@ResponseBody
public String exceptionHandler(Exception e) {
logger.error(e.getMessage(), e);
return e.getMessage();
}
}

這裡模擬正式業務的異常觸發,隨機一個bool值,如果為真就拋出運行時異常

一般都會對異常統一處理,記得要使用 logger.error(e.getMessage(), e); 只是返回結果是不會發郵件的

啟動並測試

訪問 http://localhost:8080/app

要訪問到頁面顯示"查詢報錯"

異常郵件截圖

架構實戰篇(十三):Spring Boot Logback 郵件通知

收件箱

架構實戰篇(十三):Spring Boot Logback 郵件通知


分享到:


相關文章: