-
Notifications
You must be signed in to change notification settings - Fork 0
/
logsnag.go
executable file
·123 lines (100 loc) · 2.34 KB
/
logsnag.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package logsnag
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type LogSnag struct {
Token string
Project string
}
func (logsnag *LogSnag) GetProject() string {
return logsnag.Project
}
func (logsnag *LogSnag) Publish(channel string, event string, icon string, tags map[string]any, notify bool) bool {
url := "https://api.logsnag.com/v1/log"
method := "POST"
// Create the description string from map
var pairs []string
for key, value := range tags {
pairs = append(pairs, fmt.Sprintf(`%s: %v`, key, value))
}
description := strings.Join(pairs, ", ")
payload := strings.NewReader(`{
"project": "` + logsnag.GetProject() + `",
"channel": "` + channel + `",
"event": "` + event + `",
"description": "` + description + `",
"icon": "` + icon + `",
"notify": "` + strconv.FormatBool(notify) + `"
}`)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return false
}
req.Header.Add("Authorization", "Bearer "+logsnag.Token)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return false
}
defer res.Body.Close()
_, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func (logsnag *LogSnag) Insight(title string, value string, icon string) bool {
url := "https://api.logsnag.com/v1/insight"
method := "POST"
payload := strings.NewReader(`{
"project": "` + logsnag.GetProject() + `",
"title": "` + title + `",
"value": "` + value + `",
"icon": "` + icon + `"
}`)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return false
}
req.Header.Add("Authorization", "Bearer "+logsnag.Token)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return false
}
defer res.Body.Close()
_, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func NewLogSnag(token string, project string) LogSnag {
return LogSnag{
Token: token,
Project: project,
}
}
func main() {
logSnag := NewLogSnag(
"d67d3443e793dad29d9c94df76838367",
"ferry-times",
)
logSnag.Insight(
"waitlist", // Channel
"User Joined", // Event
"🛥️", // Icon
)
}