forked from Penglq/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert.go
47 lines (42 loc) · 1.02 KB
/
alert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package QLog
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type AlertApiConfig struct {
AppId string
URL string
ContentType string
Format string
IsAlert bool
}
type alertApiData struct {
AppId string `json:"appId"`
Content string `json:"content"`
Priority string `json:"priority"`
}
//"alert"
func (l *logger) alert(s string, priority string) {
if l.alertURL != "" {
client := http.Client{}
data := &alertApiData{l.alertAppId, s, priority}
str, err := json.Marshal(data)
resp, err := client.Post(l.alertURL, l.alertContentType, strings.NewReader(string(str)))
if err != nil {
l.Error("alertApi alert", err.Error())
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
l.Error("alert service read body error", err.Error())
} else {
l.Info("alert service body", string(body))
}
if resp.StatusCode != http.StatusOK {
l.Info("alert service http error", resp.StatusCode, "body", string(body))
}
}
}
}