-
Notifications
You must be signed in to change notification settings - Fork 0
/
structured_logger.go
150 lines (144 loc) · 4.26 KB
/
structured_logger.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package middleware
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"time"
)
type Formatter interface {
LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{})
LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww WrapResponseWriter, c LogConfig, startTime time.Time, response string, fields map[string]interface{}, includeRequest bool)
}
type StructuredLogger struct {
send func(context.Context, []byte, map[string]string) error
KeyMap map[string]string
RequestKey string
JsonFormat bool
}
var fieldConfig FieldConfig
func NewLogger() *StructuredLogger {
return &StructuredLogger{}
}
func NewLoggerWithJsonFormat(requestKey string, jsonFormat bool) *StructuredLogger {
return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat}
}
func NewLoggerWithSending(requestKey string, jsonFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *StructuredLogger {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat, send: send, KeyMap: keyMap}
}
func (l *StructuredLogger) LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww WrapResponseWriter,
c LogConfig, t1 time.Time, response string, fields map[string]interface{}, includeRequest bool) {
BuildResponseBody(ww, c, t1, response, fields, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
func (l *StructuredLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{}) {
msg := "Request " + r.Method + " " + r.RequestURI
if l.JsonFormat && len(l.RequestKey) > 0 {
req, ok := fields[l.RequestKey]
if ok {
requestBody, ok2 := req.(string)
if ok2 {
requestMap := map[string]interface{}{}
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
fields[l.RequestKey] = requestMap
}
}
}
}
log(r.Context(), msg, fields)
if l.send != nil {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
func BuildResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isJsonFormat bool) {
if len(c.Response) > 0 {
if isJsonFormat {
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
fields[c.Response] = responseMap
} else {
fields[c.Response] = response
}
} else {
fields[c.Response] = response
}
}
if isJsonFormat && len(c.Request) > 0 {
req, ok := fields[c.Request]
if ok {
requestBody, ok2 := req.(string)
if ok2 {
requestMap := map[string]interface{}{}
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
fields[c.Request] = requestMap
}
}
}
}
if len(c.ResponseStatus) > 0 {
fields[c.ResponseStatus] = ww.Status()
}
if len(fieldConfig.Duration) > 0 {
t2 := time.Now()
duration := t2.Sub(t1)
fields[fieldConfig.Duration] = duration.Milliseconds()
}
if len(c.Size) > 0 {
fields[c.Size] = ww.BytesWritten()
}
}
func BuildRequestBody(r *http.Request, request string, fields map[string]interface{}) {
if len(request) == 0 {
return
}
if r.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
fields[request] = buf.String()
r.Body = io.NopCloser(buf)
}
}
func Send(ctx context.Context, send func(context.Context, []byte, map[string]string) error, msg string, fields map[string]interface{}, keyMap map[string]string) {
m2 := AddKeyFields(msg, fields, keyMap)
b, err := json.Marshal(m2)
if err == nil {
send(ctx, b, nil)
}
}
func AddKeyFields(message string, m map[string]interface{}, keys map[string]string) map[string]interface{} {
level := "level"
t := "time"
msg := "msg"
if keys != nil {
ks := keys
v1, ok1 := ks[level]
if ok1 && len(v1) > 0 {
level = v1
}
v2, ok2 := ks[t]
if ok2 && len(v2) > 0 {
t = v2
}
v3, ok3 := ks[msg]
if ok3 && len(v3) > 0 {
msg = v3
}
}
m[msg] = message
m[level] = "info"
m[t] = time.Now()
return m
}