-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastilog.go
127 lines (115 loc) · 2.86 KB
/
elastilog.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
124
125
126
127
package log
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/treetopllc/elastilog"
)
type ElasticClient struct {
Basic
client elastilog.Client
}
func NewElastic(uri string, tags ...string) *ElasticClient {
c := &ElasticClient{
client: elastilog.NewClient(uri, tags...),
}
c.Basic = NewBasic(c, "", 0)
return c
}
func (el ElasticClient) Send(e *ElasticEntry) {
if e != nil {
e.entry.Attributes["level"] = string(e.level)
e.entry.Log = string(e.str)
el.client.Send(e.entry)
}
}
func (el ElasticClient) Write(msg []byte) (int, error) {
hostname, _ := os.Hostname()
el.client.Send(elastilog.Entry{
Log: string(msg),
Host: hostname,
Timestamp: time.Now(),
Attributes: elastilog.Attributes{"level": "std"},
})
return len(msg), nil
}
type ElasticEntry struct {
level LogLevel
str StringLogger
entry elastilog.Entry
}
func NewElasticEntry() *ElasticEntry {
hostname, _ := os.Hostname()
return &ElasticEntry{
level: INFO,
entry: elastilog.Entry{
Host: hostname,
Timestamp: time.Now(),
Attributes: make(elastilog.Attributes),
},
}
}
func (ee *ElasticEntry) set(key string, value string) {
ee.entry.Attributes[key] = value
}
func (ee *ElasticEntry) SetRequest(req *http.Request) {
ee.entry.Host = req.URL.Host
ee.set("request.method", req.Method)
ee.set("request.path", req.URL.Path)
ee.set("request.query", req.URL.RawQuery)
ee.set("request.proto", req.Proto)
if req.Body != nil {
rb := requestBody(req)
ee.set("request.body", rb+" ")
}
for k, h := range req.Header {
ee.set("request.header."+k, strings.Join(h, ","))
}
}
func (ee *ElasticEntry) SetUserID(id string) {
ee.set("request.user_id", id)
}
func (ee *ElasticEntry) SetProductType(pt string) {
ee.set("request.product_type", pt)
}
func (ee *ElasticEntry) SetRequestID(id string) {
ee.set("request.id", id)
}
func (ee *ElasticEntry) SetResponse(status int, body interface{}) {
if debug || status >= 300 {
ee.set("response.body", responseBody(body))
}
ee.set("response.status", fmt.Sprintf("%v", status))
ee.set("duration", fmt.Sprintf("%v", time.Since(ee.entry.Timestamp).Nanoseconds()/1000000)) //1 ms = 1000000ns
}
func (ee *ElasticEntry) Write(b []byte) (int, error) {
ee.level.Set(INFO)
ee.str.Log(string(b))
return len(b), nil
}
func (ee *ElasticEntry) Log(msg ...interface{}) {
ee.level.Set(INFO)
ee.str.Log(msg...)
}
func (ee *ElasticEntry) Logf(msg string, args ...interface{}) {
ee.level.Set(INFO)
ee.str.Logf(msg, args...)
}
func (ee *ElasticEntry) Error(err error) {
ee.level.Set(ERROR)
ee.str.Error(err)
}
func (ee *ElasticEntry) Errorf(msg string, args ...interface{}) {
ee.level.Set(ERROR)
ee.str.Errorf(msg, args...)
}
func (ee *ElasticEntry) Panic(msg interface{}) {
ee.level.Set(PANIC)
ee.str.Panic(msg)
}
func (ee *ElasticEntry) Panicf(msg string, args ...interface{}) {
ee.level.Set(PANIC)
ee.str.Panicf(msg, args...)
}