-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrus_http.go
51 lines (41 loc) · 1.18 KB
/
logrus_http.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
package logrus_http
import (
"net/http"
"net/url"
"github.com/Sirupsen/logrus"
)
type HttpHook struct {
RequestEndpoint string
RequestFormKey string
RequestExtraFields map[string]string
LogExtraFields map[string]interface{}
}
// Creates a hook to be added to an instance of logger. This is called with
// `hook, err := NewHttpHook("http://log-server/post_new_log", "logBody")`
// `if err == nil { log.Hooks.Add(hook) }`
func NewHttpHook(endpoint string, formKey string, requestExtraFields map[string]string,
extraLogFields map[string]interface{}) (*HttpHook, error) {
return &HttpHook{endpoint, formKey, requestExtraFields, extraLogFields}, nil
}
func (hook *HttpHook) Fire(entry *logrus.Entry) error {
line, err := entry.WithFields(hook.LogExtraFields).String()
if err != nil {
return err
}
reqForm := url.Values{}
// add in extra fields, if any
for k, v := range hook.RequestExtraFields {
reqForm.Set(k, v)
}
// add log line
reqForm.Set(hook.RequestFormKey, line)
resp, err := http.PostForm(hook.RequestEndpoint, reqForm)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func (hook *HttpHook) Levels() []logrus.Level {
return logrus.AllLevels
}