Skip to content

Commit d79926e

Browse files
committed
Refactor code
1 parent 546bc90 commit d79926e

File tree

11 files changed

+502
-16
lines changed

11 files changed

+502
-16
lines changed

echo/log.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ type EchoLogger struct {
1919
Mask func(fieldName, s string) string
2020
}
2121

22-
func NewEchoLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), mask func(fieldName, s string) string) *EchoLogger {
23-
logger := NewLogger()
24-
return &EchoLogger{c, logInfo, logger, mask}
22+
func NewEchoLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), f Formatter, mask func(fieldName, s string) string) *EchoLogger {
23+
return &EchoLogger{c, logInfo, f, mask}
2524
}
2625

2726
func (l *EchoLogger) Logger(next echo.HandlerFunc) echo.HandlerFunc {

echo/mask_logger.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package echo
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"strings"
11+
"time"
12+
)
13+
14+
type MaskLogger struct {
15+
send func(ctx context.Context, data []byte, attributes map[string]string) (string, error)
16+
KeyMap map[string]string
17+
Goroutines bool
18+
MaskRequest func(fieldName string, v interface{}) interface{}
19+
MaskResponse func(fieldName string, v interface{}) interface{}
20+
}
21+
22+
func NewMaskLogger(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}) *MaskLogger {
23+
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse}
24+
}
25+
func NewMaskLoggerWithSending(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}, send func(context.Context, []byte, map[string]string) (string, error), goroutines bool, options ...map[string]string) *MaskLogger {
26+
var keyMap map[string]string
27+
if len(options) >= 1 {
28+
keyMap = options[0]
29+
}
30+
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse, send: send, Goroutines: goroutines, KeyMap: keyMap}
31+
}
32+
func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww WrapResponseWriter,
33+
c LogConfig, t1 time.Time, response string, fields map[string]interface{}, singleLog bool) {
34+
fs := BuildMaskedResponseBody(ww, c, t1, response, fields, l.MaskResponse)
35+
var msg string
36+
if singleLog {
37+
msg = r.Method + " " + r.RequestURI
38+
} else {
39+
msg = "Response " + r.Method + " " + r.RequestURI
40+
}
41+
log(r.Context(), msg, fs)
42+
if l.send != nil {
43+
if l.Goroutines {
44+
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
45+
} else {
46+
Send(r.Context(), l.send, msg, fields, l.KeyMap)
47+
}
48+
}
49+
}
50+
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, c LogConfig, fields map[string]interface{}, singleLog bool) {
51+
var fs map[string]interface{}
52+
fs = fields
53+
if len(c.Request) > 0 && r.Method != "GET" && r.Method != "DELETE" && !strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
54+
fs = BuildMaskedRequestBody(r, c.Request, fields, l.MaskRequest)
55+
}
56+
if !singleLog {
57+
msg := "Request " + r.Method + " " + r.RequestURI
58+
log(r.Context(), msg, fs)
59+
if l.send != nil {
60+
if l.Goroutines {
61+
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
62+
} else {
63+
Send(r.Context(), l.send, msg, fields, l.KeyMap)
64+
}
65+
}
66+
}
67+
}
68+
69+
func BuildMaskedResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
70+
if len(c.Response) > 0 {
71+
fields[c.Response] = response
72+
responseBody := response
73+
responseMap := map[string]interface{}{}
74+
json.Unmarshal([]byte(responseBody), &responseMap)
75+
if len(responseMap) > 0 {
76+
for key, v := range responseMap {
77+
responseMap[key] = mask(key, v)
78+
}
79+
responseString, err := json.Marshal(responseMap)
80+
if err != nil {
81+
fmt.Printf("Error: %s", err.Error())
82+
} else {
83+
fields[c.Response] = string(responseString)
84+
}
85+
}
86+
}
87+
if len(c.ResponseStatus) > 0 {
88+
fields[c.ResponseStatus] = ww.Status()
89+
}
90+
if len(fieldConfig.Duration) > 0 {
91+
t2 := time.Now()
92+
duration := t2.Sub(t1)
93+
fields[fieldConfig.Duration] = duration.Milliseconds()
94+
}
95+
if len(c.Size) > 0 {
96+
fields[c.Size] = ww.BytesWritten()
97+
}
98+
return fields
99+
}
100+
func BuildMaskedRequestBody(r *http.Request, request string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
101+
if r.Body != nil {
102+
buf := new(bytes.Buffer)
103+
buf.ReadFrom(r.Body)
104+
fields[request] = buf.String()
105+
r.Body = ioutil.NopCloser(buf)
106+
requestBody := fields[request].(string)
107+
requestMap := map[string]interface{}{}
108+
json.Unmarshal([]byte(requestBody), &requestMap)
109+
if len(requestMap) > 0 {
110+
for key, v := range requestMap {
111+
requestMap[key] = mask(key, v)
112+
}
113+
requestString, err := json.Marshal(requestMap)
114+
if err != nil {
115+
fmt.Printf("Error: %s", err.Error())
116+
} else {
117+
fields[request] = string(requestString)
118+
}
119+
}
120+
}
121+
return fields
122+
}

echo/v3/log.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ type EchoLogger struct {
1919
Mask func(fieldName, s string) string
2020
}
2121

22-
func NewEchoLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), mask func(fieldName, s string) string) *EchoLogger {
23-
logger := NewLogger()
24-
return &EchoLogger{c, logInfo, logger, mask}
22+
func NewEchoLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), f Formatter, mask func(fieldName, s string) string) *EchoLogger {
23+
return &EchoLogger{c, logInfo, f, mask}
2524
}
2625

2726
func (l *EchoLogger) Logger(next echo.HandlerFunc) echo.HandlerFunc {

echo/v3/mask_logger.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package echo
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"strings"
11+
"time"
12+
)
13+
14+
type MaskLogger struct {
15+
send func(ctx context.Context, data []byte, attributes map[string]string) (string, error)
16+
KeyMap map[string]string
17+
Goroutines bool
18+
MaskRequest func(fieldName string, v interface{}) interface{}
19+
MaskResponse func(fieldName string, v interface{}) interface{}
20+
}
21+
22+
func NewMaskLogger(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}) *MaskLogger {
23+
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse}
24+
}
25+
func NewMaskLoggerWithSending(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}, send func(context.Context, []byte, map[string]string) (string, error), goroutines bool, options ...map[string]string) *MaskLogger {
26+
var keyMap map[string]string
27+
if len(options) >= 1 {
28+
keyMap = options[0]
29+
}
30+
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse, send: send, Goroutines: goroutines, KeyMap: keyMap}
31+
}
32+
func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww WrapResponseWriter,
33+
c LogConfig, t1 time.Time, response string, fields map[string]interface{}, singleLog bool) {
34+
fs := BuildMaskedResponseBody(ww, c, t1, response, fields, l.MaskResponse)
35+
var msg string
36+
if singleLog {
37+
msg = r.Method + " " + r.RequestURI
38+
} else {
39+
msg = "Response " + r.Method + " " + r.RequestURI
40+
}
41+
log(r.Context(), msg, fs)
42+
if l.send != nil {
43+
if l.Goroutines {
44+
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
45+
} else {
46+
Send(r.Context(), l.send, msg, fields, l.KeyMap)
47+
}
48+
}
49+
}
50+
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, c LogConfig, fields map[string]interface{}, singleLog bool) {
51+
var fs map[string]interface{}
52+
fs = fields
53+
if len(c.Request) > 0 && r.Method != "GET" && r.Method != "DELETE" && !strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
54+
fs = BuildMaskedRequestBody(r, c.Request, fields, l.MaskRequest)
55+
}
56+
if !singleLog {
57+
msg := "Request " + r.Method + " " + r.RequestURI
58+
log(r.Context(), msg, fs)
59+
if l.send != nil {
60+
if l.Goroutines {
61+
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
62+
} else {
63+
Send(r.Context(), l.send, msg, fields, l.KeyMap)
64+
}
65+
}
66+
}
67+
}
68+
69+
func BuildMaskedResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
70+
if len(c.Response) > 0 {
71+
fields[c.Response] = response
72+
responseBody := response
73+
responseMap := map[string]interface{}{}
74+
json.Unmarshal([]byte(responseBody), &responseMap)
75+
if len(responseMap) > 0 {
76+
for key, v := range responseMap {
77+
responseMap[key] = mask(key, v)
78+
}
79+
responseString, err := json.Marshal(responseMap)
80+
if err != nil {
81+
fmt.Printf("Error: %s", err.Error())
82+
} else {
83+
fields[c.Response] = string(responseString)
84+
}
85+
}
86+
}
87+
if len(c.ResponseStatus) > 0 {
88+
fields[c.ResponseStatus] = ww.Status()
89+
}
90+
if len(fieldConfig.Duration) > 0 {
91+
t2 := time.Now()
92+
duration := t2.Sub(t1)
93+
fields[fieldConfig.Duration] = duration.Milliseconds()
94+
}
95+
if len(c.Size) > 0 {
96+
fields[c.Size] = ww.BytesWritten()
97+
}
98+
return fields
99+
}
100+
func BuildMaskedRequestBody(r *http.Request, request string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
101+
if r.Body != nil {
102+
buf := new(bytes.Buffer)
103+
buf.ReadFrom(r.Body)
104+
fields[request] = buf.String()
105+
r.Body = ioutil.NopCloser(buf)
106+
requestBody := fields[request].(string)
107+
requestMap := map[string]interface{}{}
108+
json.Unmarshal([]byte(requestBody), &requestMap)
109+
if len(requestMap) > 0 {
110+
for key, v := range requestMap {
111+
requestMap[key] = mask(key, v)
112+
}
113+
requestString, err := json.Marshal(requestMap)
114+
if err != nil {
115+
fmt.Printf("Error: %s", err.Error())
116+
} else {
117+
fields[request] = string(requestString)
118+
}
119+
}
120+
}
121+
return fields
122+
}

gin/log.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ type GinLogger struct {
1919
Mask func(fieldName, s string) string
2020
}
2121

22-
func NewGinLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), mask func(fieldName, s string) string) *GinLogger {
23-
logger := NewLogger()
24-
return &GinLogger{c, logInfo, logger, mask}
22+
func NewGinLogger(c LogConfig, logInfo func(ctx context.Context, msg string, fields map[string]interface{}), f Formatter, mask func(fieldName, s string) string) *GinLogger {
23+
return &GinLogger{c, logInfo, f, mask}
2524
}
2625

2726
func (l *GinLogger) Logger() gin.HandlerFunc {

0 commit comments

Comments
 (0)