-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotimber.go
109 lines (92 loc) · 2.59 KB
/
gotimber.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
package gotimber
import (
"encoding/base64"
"encoding/json"
"time"
)
const (
_warning = "warning"
_debug = "debug"
_info = "info"
_notice = "notice"
_error = "error"
_critical = "critical"
_alert = "alert"
_emergency = "emergency"
)
type GoTimber struct {
apiKey string
client *HttpClient
}
type goTimberJson struct {
Dt string `json:"dt"`
Level string `json:"level"`
Message string `json:"message"`
Context interface{} `json:"context,omitempty"`
Event interface{} `json:"event,omitempty"`
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func NewGoTimber(apiKey string) *GoTimber {
encodedAPIKey := base64.StdEncoding.EncodeToString([]byte(apiKey[:]))
goTimber := &GoTimber{apiKey: encodedAPIKey}
goTimber.client = &HttpClient{address: "https://logs.timber.io/frames", gotimber: goTimber}
return goTimber
}
func prepareLogDataToSend(message string, level string) []byte {
goTimberStruct := &goTimberJson{Dt: time.Now().String(), Level: level, Message: message}
goTimberJSON, err := json.Marshal(goTimberStruct)
checkError(err)
return goTimberJSON
}
func (t *GoTimber) Warning(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _warning)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Debug(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _debug)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Info(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _info)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Notice(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _notice)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Error(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _error)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Critical(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _critical)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Alert(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _alert)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) Emergency(message string) *GoTimber {
goTimberJSON := prepareLogDataToSend(message, _emergency)
t.client.postLog(goTimberJSON)
return t
}
func (t *GoTimber) GetTimberStructure() *goTimberJson {
return &goTimberJson{}
}
func (t *GoTimber) SendCustomTimberStructure(timberStructure *goTimberJson) {
timberJson, err := json.Marshal(timberStructure)
checkError(err)
t.client.postLog(timberJson)
}