mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
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")
|
|
}
|
|
}
|
|
}
|