Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Sep 30, 2023
1 parent 546bc90 commit d79926e
Show file tree
Hide file tree
Showing 11 changed files with 502 additions and 16 deletions.
5 changes: 2 additions & 3 deletions echo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type EchoLogger struct {
Mask func(fieldName, s string) string
}

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

func (l *EchoLogger) Logger(next echo.HandlerFunc) echo.HandlerFunc {
Expand Down
122 changes: 122 additions & 0 deletions echo/mask_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package echo

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)

type MaskLogger struct {
send func(ctx context.Context, data []byte, attributes map[string]string) (string, error)
KeyMap map[string]string
Goroutines bool
MaskRequest func(fieldName string, v interface{}) interface{}
MaskResponse func(fieldName string, v interface{}) interface{}
}

func NewMaskLogger(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}) *MaskLogger {
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse}
}
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 {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse, send: send, Goroutines: goroutines, KeyMap: keyMap}
}
func (l *MaskLogger) 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{}, singleLog bool) {
fs := BuildMaskedResponseBody(ww, c, t1, response, fields, l.MaskResponse)
var msg string
if singleLog {
msg = r.Method + " " + r.RequestURI
} else {
msg = "Response " + r.Method + " " + r.RequestURI
}
log(r.Context(), msg, fs)
if l.send != nil {
if l.Goroutines {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
} else {
Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, c LogConfig, fields map[string]interface{}, singleLog bool) {
var fs map[string]interface{}
fs = fields
if len(c.Request) > 0 && r.Method != "GET" && r.Method != "DELETE" && !strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
fs = BuildMaskedRequestBody(r, c.Request, fields, l.MaskRequest)
}
if !singleLog {
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fs)
if l.send != nil {
if l.Goroutines {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
} else {
Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
}
}

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{} {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
for key, v := range responseMap {
responseMap[key] = mask(key, v)
}
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
}
}
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()
}
return fields
}
func BuildMaskedRequestBody(r *http.Request, request string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
if r.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
fields[request] = buf.String()
r.Body = ioutil.NopCloser(buf)
requestBody := fields[request].(string)
requestMap := map[string]interface{}{}
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
for key, v := range requestMap {
requestMap[key] = mask(key, v)
}
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
}
}
return fields
}
File renamed without changes.
5 changes: 2 additions & 3 deletions echo/v3/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type EchoLogger struct {
Mask func(fieldName, s string) string
}

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

func (l *EchoLogger) Logger(next echo.HandlerFunc) echo.HandlerFunc {
Expand Down
122 changes: 122 additions & 0 deletions echo/v3/mask_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package echo

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)

type MaskLogger struct {
send func(ctx context.Context, data []byte, attributes map[string]string) (string, error)
KeyMap map[string]string
Goroutines bool
MaskRequest func(fieldName string, v interface{}) interface{}
MaskResponse func(fieldName string, v interface{}) interface{}
}

func NewMaskLogger(maskRequest func(fieldName string, v interface{}) interface{}, maskResponse func(fieldName string, v interface{}) interface{}) *MaskLogger {
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse}
}
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 {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
return &MaskLogger{MaskRequest: maskRequest, MaskResponse: maskResponse, send: send, Goroutines: goroutines, KeyMap: keyMap}
}
func (l *MaskLogger) 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{}, singleLog bool) {
fs := BuildMaskedResponseBody(ww, c, t1, response, fields, l.MaskResponse)
var msg string
if singleLog {
msg = r.Method + " " + r.RequestURI
} else {
msg = "Response " + r.Method + " " + r.RequestURI
}
log(r.Context(), msg, fs)
if l.send != nil {
if l.Goroutines {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
} else {
Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, c LogConfig, fields map[string]interface{}, singleLog bool) {
var fs map[string]interface{}
fs = fields
if len(c.Request) > 0 && r.Method != "GET" && r.Method != "DELETE" && !strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
fs = BuildMaskedRequestBody(r, c.Request, fields, l.MaskRequest)
}
if !singleLog {
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fs)
if l.send != nil {
if l.Goroutines {
go Send(r.Context(), l.send, msg, fields, l.KeyMap)
} else {
Send(r.Context(), l.send, msg, fields, l.KeyMap)
}
}
}
}

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{} {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
for key, v := range responseMap {
responseMap[key] = mask(key, v)
}
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
}
}
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()
}
return fields
}
func BuildMaskedRequestBody(r *http.Request, request string, fields map[string]interface{}, mask func(fieldName string, s interface{}) interface{}) map[string]interface{} {
if r.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
fields[request] = buf.String()
r.Body = ioutil.NopCloser(buf)
requestBody := fields[request].(string)
requestMap := map[string]interface{}{}
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
for key, v := range requestMap {
requestMap[key] = mask(key, v)
}
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
}
}
return fields
}
File renamed without changes.
5 changes: 2 additions & 3 deletions gin/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type GinLogger struct {
Mask func(fieldName, s string) string
}

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

func (l *GinLogger) Logger() gin.HandlerFunc {
Expand Down
Loading

0 comments on commit d79926e

Please sign in to comment.