-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.go
49 lines (41 loc) · 942 Bytes
/
net.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
package logger
import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
)
type NetOutput struct {
URL string
}
func AddNetOutput(URL string, level Level, format ...Format) Output {
output := Output{NetOutput{URL: URL}, level, JSON, ""}
if len(format) > 0 {
output.Format = format[0]
}
outputs = append(outputs, output)
return output
}
func (o NetOutput) Write(p []byte) (n int, err error) {
n = len(p)
if o.URL == "" {
err = fmt.Errorf("URL not set")
return
}
req, err := http.NewRequest("POST", o.URL, bytes.NewReader(p))
if err != nil {
err = fmt.Errorf("failed to create request with log message to %s: %w", o.URL, err)
return
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
res, err := client.Do(req)
if err != nil {
err = fmt.Errorf("failed to send log message to %s: %w", o.URL, err)
return
}
defer res.Body.Close()
return
}