Skip to content

Commit

Permalink
Add: 自定义告警
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Nov 3, 2019
1 parent e79fcb5 commit 0ab7960
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions app/util/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package util

import (
"fmt"
"go-gin-api/app/config"
"go-gin-api/app/route/middleware/exception"
"log"
"os"
"runtime/debug"
"strings"
"time"
)

type errorString struct {
s string
}

func (e *errorString) Error() string {
return e.s
}

func ErrorNew (text string) error {
alarm("INFO", text)
return &errorString{text}
}

// 发邮件
func ErrorMail (text string) error {
alarm("MAIL",text)
return &errorString{text}
}

// 发短信
func ErrorSms (text string) error {
alarm("SMS", text)
return &errorString{text}
}

// 发微信
func ErrorWeChat (text string) error {
alarm("WX", text)
return &errorString{text}
}

// 告警方法
func alarm(level string, str string) {
if level == "MAIL" {
DebugStack := ""
for _, v := range strings.Split(string(debug.Stack()), "\n") {
DebugStack += v + "<br>"
}

subject := fmt.Sprintf("【系统告警】%s 项目出错了!", config.AppName)

body := strings.ReplaceAll(exception.MailTemplate, "{ErrorMsg}", fmt.Sprintf("%s", str))
body = strings.ReplaceAll(body, "{RequestTime}", GetCurrentDate())
body = strings.ReplaceAll(body, "{RequestURL}", "--")
body = strings.ReplaceAll(body, "{RequestUA}", "--")
body = strings.ReplaceAll(body, "{RequestIP}", "--")
body = strings.ReplaceAll(body, "{DebugStack}", DebugStack)

// 执行发邮件
_ = SendMail(config.ErrorNotifyUser, subject, body)

} else if level == "SMS" {
// 执行发短信

} else if level == "WX" {
// 执行发微信

} else if level == "INFO" {
// 执行记日志

if f, err := os.OpenFile(config.AppErrorLogName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil {
log.Println(err)
} else {
errorLogMap := make(map[string]interface{})
errorLogMap["time"] = time.Now().Format("2006/01/02 - 15:04:05")
errorLogMap["info"] = str

errorLogJson, _ := JsonEncode(errorLogMap)
_, _ = f.WriteString(errorLogJson + "\n")
}
}
}
Empty file added log/go-gin-api-error.log
Empty file.

0 comments on commit 0ab7960

Please sign in to comment.