-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_mildware.go
131 lines (128 loc) · 3.09 KB
/
logger_mildware.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
package middleware
import (
"context"
"net"
"net/http"
"strings"
"time"
)
func InitializeFieldConfig(c LogConfig) {
if len(c.Duration) > 0 {
fieldConfig.Duration = c.Duration
} else {
fieldConfig.Duration = "duration"
}
fieldConfig.Log = c.Log
fieldConfig.Ip = c.Ip
if c.Map != nil && len(c.Map) > 0 {
fieldConfig.Map = c.Map
}
if c.Constants != nil && len(c.Constants) > 0 {
fieldConfig.Constants = c.Constants
}
if c.Headers != nil && len(c.Headers) > 0 {
fieldConfig.Headers = c.Headers
}
if len(c.Fields) > 0 {
fields := strings.Split(c.Fields, ",")
fieldConfig.Fields = fields
}
if len(c.Masks) > 0 {
fields := strings.Split(c.Masks, ",")
fieldConfig.Masks = fields
}
if len(c.Skips) > 0 {
fields := strings.Split(c.Skips, ",")
fieldConfig.Skips = fields
}
}
func Logger(c LogConfig, log func(ctx context.Context, msg string, fields map[string]interface{}), f Formatter) func(h http.Handler) http.Handler {
InitializeFieldConfig(c)
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if !fieldConfig.Log || InSkipList(r, fieldConfig.Skips) {
h.ServeHTTP(w, r)
} else {
dw := NewResponseWriter(w)
ww := NewWrapResponseWriter(dw, r.ProtoMajor)
startTime := time.Now()
fields := BuildLogFields(c, r)
includeRequest := !c.Separate
if r.Method == "GET" || r.Method == "DELETE" || strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
includeRequest = true
} else {
BuildRequestBody(r, c.Request, fields)
}
if !includeRequest {
go f.LogRequest(log, r, fields)
}
defer func() {
if includeRequest {
go f.LogResponse(log, r, ww, c, startTime, dw.Body.String(), fields, includeRequest)
} else {
resFields := BuildLogFields(c, r)
go f.LogResponse(log, r, ww, c, startTime, dw.Body.String(), resFields, includeRequest)
}
}()
h.ServeHTTP(ww, r)
}
}
return http.HandlerFunc(fn)
}
}
func InSkipList(r *http.Request, skips []string) bool {
if skips == nil || len(skips) == 0 {
return false
}
for _, s := range skips {
if strings.HasSuffix(s, r.RequestURI) {
return true
}
}
return false
}
func BuildLogFields(c LogConfig, r *http.Request) map[string]interface{} {
fields := make(map[string]interface{}, 0)
if !c.Build {
return fields
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if len(c.Uri) > 0 {
fields[c.Uri] = r.RequestURI
}
if len(c.ReqId) > 0 {
if reqID := GetReqID(r.Context()); reqID != "" {
fields[c.ReqId] = reqID
}
}
if len(c.Scheme) > 0 {
fields[c.Scheme] = scheme
}
if len(c.Proto) > 0 {
fields[c.Proto] = r.Proto
}
if len(c.UserAgent) > 0 {
fields[c.UserAgent] = r.UserAgent()
}
if len(c.RemoteAddr) > 0 {
fields[c.RemoteAddr] = r.RemoteAddr
}
if len(c.Method) > 0 {
fields[c.Method] = r.Method
}
if len(c.RemoteIp) > 0 {
remoteIP := getRemoteIp(r)
fields[c.RemoteIp] = remoteIP
}
return fields
}
func getRemoteIp(r *http.Request) string {
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
remoteIP = r.RemoteAddr
}
return remoteIP
}