forked from xinliangnote/go-gin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e79fcb5
commit 0ab7960
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.