diff --git a/README.md b/README.md
index 224eb60..0250217 100644
--- a/README.md
+++ b/README.md
@@ -46,8 +46,8 @@ Middleware logging is a technique used in software development, particularly in
### Middleware Integration
#### Features
- Middleware Function: Designed to integrate seamlessly with existing Go libraries: [Echo](https://github.com/labstack/echo), [Gin](https://github.com/gin-gonic/gin), or net/http ([Gorilla mux](https://github.com/gorilla/mux), [Go-chi](https://github.com/go-chi/chi)).
- - Sample for [Echo](https://github.com/labstack/echo) is at [go-sql-echo-sample](https://github.com/go-tutorials/go-sql-echo-sample)
- - Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-sql-gin-sample](https://github.com/go-tutorials/go-sql-gin-sample)
+ - Sample for [Echo](https://github.com/labstack/echo) is at [go-echo-sql-sample](https://github.com/go-tutorials/go-echo-sql-sample)
+ - Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-gin-sql-sample](https://github.com/go-tutorials/go-gin-sql-sample)
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
- Context Handling: Pass context to handle request-specific data throughout the middleware chain.
#### Benefits
@@ -71,8 +71,8 @@ Middleware logging is a technique used in software development, particularly in
- Consistency: Ensures that sensitive data is consistently encrypted or masked across all logged requests and responses
#### Samples:
-- Sample for [Echo](https://github.com/labstack/echo) is at [go-sql-echo-sample](https://github.com/go-tutorials/go-sql-echo-sample)
-- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-sql-gin-sample](https://github.com/go-tutorials/go-sql-gin-sample)
+- Sample for [Echo](https://github.com/labstack/echo) is at [go-echo-sql-sample](https://github.com/go-tutorials/go-echo-sql-sample)
+- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-gin-sql-sample](https://github.com/go-tutorials/go-gin-sql-sample)
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
### Enable/Disable Logging
diff --git a/echo/mask_logger.go b/echo/mask_logger.go
index 20589dc..3578cbc 100644
--- a/echo/mask_logger.go
+++ b/echo/mask_logger.go
@@ -14,25 +14,25 @@ type MaskLogger struct {
RequestKey string
MaskRequest func(map[string]interface{})
MaskResponse func(map[string]interface{})
- StringFormat bool
+ JsonFormat bool
}
-func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool) *MaskLogger {
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat}
+func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger {
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat}
}
-func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
+func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat, send: send, 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{}, includeRequest bool) {
if includeRequest && len(c.Request) > 0 {
- MaskRequest(c.Request, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(c.Request, fields, l.MaskRequest, l.JsonFormat)
}
- MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.StringFormat)
+ MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -40,7 +40,7 @@ func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]in
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{}) {
- MaskRequest(l.RequestKey, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(l.RequestKey, fields, l.MaskRequest, l.JsonFormat)
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -48,7 +48,7 @@ func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]int
}
}
-func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
@@ -56,15 +56,15 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
mask(responseMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[c.Response] = responseMap
+ } else {
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
- } else {
- fields[c.Response] = responseMap
}
}
}
@@ -80,7 +80,7 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
fields[c.Size] = ww.BytesWritten()
}
}
-func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(request) > 0 {
req, ok := fields[request]
if ok {
@@ -90,15 +90,15 @@ func MaskRequest(request string, fields map[string]interface{}, mask func(map[st
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
mask(requestMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[request] = requestMap
+ } else {
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
- } else {
- fields[request] = requestMap
}
}
}
diff --git a/echo/structured_logger.go b/echo/structured_logger.go
index 55614b0..679df81 100644
--- a/echo/structured_logger.go
+++ b/echo/structured_logger.go
@@ -14,10 +14,10 @@ type Formatter 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
- StringFormat bool
+ send func(context.Context, []byte, map[string]string) error
+ KeyMap map[string]string
+ RequestKey string
+ JsonFormat bool
}
var fieldConfig FieldConfig
@@ -25,19 +25,19 @@ var fieldConfig FieldConfig
func NewLogger() *StructuredLogger {
return &StructuredLogger{}
}
-func NewLoggerWithStringFormat(requestKey string, stringFormat bool) *StructuredLogger {
- return &StructuredLogger{RequestKey: requestKey, StringFormat: stringFormat}
+func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *StructuredLogger {
+ return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat}
}
-func NewLoggerWithSending(requestKey string, stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *StructuredLogger {
+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, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ 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) {
- BuildResponse(ww, c, t1, response, fields, l.StringFormat)
+ BuildResponse(ww, c, t1, response, fields, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -53,7 +53,7 @@ func Send(ctx context.Context, send func(context.Context, []byte, map[string]str
}
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.StringFormat && len(l.RequestKey) > 0 {
+ if l.JsonFormat && len(l.RequestKey) > 0 {
req, ok := fields[l.RequestKey]
if ok {
requestBody, ok2 := req.(string)
@@ -72,11 +72,9 @@ func (l *StructuredLogger) LogRequest(log func(context.Context, string, map[stri
}
}
-func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isStringFormat bool) {
+func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, jsonFormat bool) {
if len(c.Response) > 0 {
- if isStringFormat {
- fields[c.Response] = response
- } else {
+ if jsonFormat {
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
@@ -85,9 +83,11 @@ func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response st
} else {
fields[c.Response] = response
}
+ } else {
+ fields[c.Response] = response
}
}
- if !isStringFormat && len(c.Request) > 0 {
+ if jsonFormat && len(c.Request) > 0 {
req, ok := fields[c.Request]
if ok {
requestBody, ok2 := req.(string)
diff --git a/echo/v3/mask_logger.go b/echo/v3/mask_logger.go
index 20589dc..3578cbc 100644
--- a/echo/v3/mask_logger.go
+++ b/echo/v3/mask_logger.go
@@ -14,25 +14,25 @@ type MaskLogger struct {
RequestKey string
MaskRequest func(map[string]interface{})
MaskResponse func(map[string]interface{})
- StringFormat bool
+ JsonFormat bool
}
-func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool) *MaskLogger {
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat}
+func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger {
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat}
}
-func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
+func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat, send: send, 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{}, includeRequest bool) {
if includeRequest && len(c.Request) > 0 {
- MaskRequest(c.Request, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(c.Request, fields, l.MaskRequest, l.JsonFormat)
}
- MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.StringFormat)
+ MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -40,7 +40,7 @@ func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]in
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{}) {
- MaskRequest(l.RequestKey, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(l.RequestKey, fields, l.MaskRequest, l.JsonFormat)
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -48,7 +48,7 @@ func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]int
}
}
-func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
@@ -56,15 +56,15 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
mask(responseMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[c.Response] = responseMap
+ } else {
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
- } else {
- fields[c.Response] = responseMap
}
}
}
@@ -80,7 +80,7 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
fields[c.Size] = ww.BytesWritten()
}
}
-func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(request) > 0 {
req, ok := fields[request]
if ok {
@@ -90,15 +90,15 @@ func MaskRequest(request string, fields map[string]interface{}, mask func(map[st
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
mask(requestMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[request] = requestMap
+ } else {
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
- } else {
- fields[request] = requestMap
}
}
}
diff --git a/echo/v3/structured_logger.go b/echo/v3/structured_logger.go
index 55614b0..679df81 100644
--- a/echo/v3/structured_logger.go
+++ b/echo/v3/structured_logger.go
@@ -14,10 +14,10 @@ type Formatter 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
- StringFormat bool
+ send func(context.Context, []byte, map[string]string) error
+ KeyMap map[string]string
+ RequestKey string
+ JsonFormat bool
}
var fieldConfig FieldConfig
@@ -25,19 +25,19 @@ var fieldConfig FieldConfig
func NewLogger() *StructuredLogger {
return &StructuredLogger{}
}
-func NewLoggerWithStringFormat(requestKey string, stringFormat bool) *StructuredLogger {
- return &StructuredLogger{RequestKey: requestKey, StringFormat: stringFormat}
+func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *StructuredLogger {
+ return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat}
}
-func NewLoggerWithSending(requestKey string, stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *StructuredLogger {
+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, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ 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) {
- BuildResponse(ww, c, t1, response, fields, l.StringFormat)
+ BuildResponse(ww, c, t1, response, fields, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -53,7 +53,7 @@ func Send(ctx context.Context, send func(context.Context, []byte, map[string]str
}
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.StringFormat && len(l.RequestKey) > 0 {
+ if l.JsonFormat && len(l.RequestKey) > 0 {
req, ok := fields[l.RequestKey]
if ok {
requestBody, ok2 := req.(string)
@@ -72,11 +72,9 @@ func (l *StructuredLogger) LogRequest(log func(context.Context, string, map[stri
}
}
-func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isStringFormat bool) {
+func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, jsonFormat bool) {
if len(c.Response) > 0 {
- if isStringFormat {
- fields[c.Response] = response
- } else {
+ if jsonFormat {
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
@@ -85,9 +83,11 @@ func BuildResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response st
} else {
fields[c.Response] = response
}
+ } else {
+ fields[c.Response] = response
}
}
- if !isStringFormat && len(c.Request) > 0 {
+ if jsonFormat && len(c.Request) > 0 {
req, ok := fields[c.Request]
if ok {
requestBody, ok2 := req.(string)
diff --git a/gin/mask_logger.go b/gin/mask_logger.go
index 2bc73b2..c34d67d 100644
--- a/gin/mask_logger.go
+++ b/gin/mask_logger.go
@@ -14,26 +14,26 @@ type MaskLogger struct {
RequestKey string
MaskRequest func(map[string]interface{})
MaskResponse func(map[string]interface{})
- StringFormat bool
+ JsonFormat bool
}
-func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool) *MaskLogger {
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat}
+func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger {
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat}
}
-func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
+func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat, send: send, KeyMap: keyMap}
}
func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww ResponseWriter,
c LogConfig, t1 time.Time, response string, fields map[string]interface{}, includeRequest bool) {
if includeRequest && len(c.Request) > 0 {
- MaskRequest(c.Request, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(c.Request, fields, l.MaskRequest, l.JsonFormat)
}
- MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.StringFormat)
+ MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -41,7 +41,7 @@ func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]in
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{}) {
- MaskRequest(l.RequestKey, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(l.RequestKey, fields, l.MaskRequest, l.JsonFormat)
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -49,7 +49,7 @@ func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]int
}
}
-func MaskResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
@@ -57,15 +57,15 @@ func MaskResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string,
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
mask(responseMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[c.Response] = responseMap
+ } else {
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
- } else {
- fields[c.Response] = responseMap
}
}
}
@@ -81,7 +81,7 @@ func MaskResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string,
fields[c.Size] = ww.Size()
}
}
-func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(request) > 0 {
req, ok := fields[request]
if ok {
@@ -91,15 +91,15 @@ func MaskRequest(request string, fields map[string]interface{}, mask func(map[st
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
mask(requestMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[request] = requestMap
+ } else {
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
- } else {
- fields[request] = requestMap
}
}
}
diff --git a/gin/structured_logger.go b/gin/structured_logger.go
index 11dcdb4..8df8d73 100644
--- a/gin/structured_logger.go
+++ b/gin/structured_logger.go
@@ -14,10 +14,10 @@ type Formatter interface {
LogResponse(log func(context.Context, string, map[string]interface{}), r *http.Request, ww ResponseWriter, 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
- StringFormat bool
+ send func(context.Context, []byte, map[string]string) error
+ KeyMap map[string]string
+ RequestKey string
+ JsonFormat bool
}
var fieldConfig FieldConfig
@@ -25,20 +25,20 @@ var fieldConfig FieldConfig
func NewLogger() *StructuredLogger {
return &StructuredLogger{}
}
-func NewLoggerWithStringFormat(requestKey string, stringFormat bool) *StructuredLogger {
- return &StructuredLogger{RequestKey: requestKey, StringFormat: stringFormat}
+func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *StructuredLogger {
+ return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat}
}
-func NewLoggerWithSending(requestKey string, stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *StructuredLogger {
+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, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ 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 ResponseWriter,
c LogConfig, t1 time.Time, response string, fields map[string]interface{}, includeRequest bool) {
- BuildResponse(ww, c, t1, response, fields, l.StringFormat)
+ BuildResponse(ww, c, t1, response, fields, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -54,7 +54,7 @@ func Send(ctx context.Context, send func(context.Context, []byte, map[string]str
}
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.StringFormat && len(l.RequestKey) > 0 {
+ if l.JsonFormat && len(l.RequestKey) > 0 {
req, ok := fields[l.RequestKey]
if ok {
requestBody, ok2 := req.(string)
@@ -73,11 +73,9 @@ func (l *StructuredLogger) LogRequest(log func(context.Context, string, map[stri
}
}
-func BuildResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isStringFormat bool) {
+func BuildResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isJsonFormat bool) {
if len(c.Response) > 0 {
- if isStringFormat {
- fields[c.Response] = response
- } else {
+ if isJsonFormat {
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
@@ -86,9 +84,11 @@ func BuildResponse(ww ResponseWriter, c LogConfig, t1 time.Time, response string
} else {
fields[c.Response] = response
}
+ } else {
+ fields[c.Response] = response
}
}
- if !isStringFormat && len(c.Request) > 0 {
+ if isJsonFormat && len(c.Request) > 0 {
req, ok := fields[c.Request]
if ok {
requestBody, ok2 := req.(string)
diff --git a/mask_logger.go b/mask_logger.go
index 2effaf8..297b07e 100644
--- a/mask_logger.go
+++ b/mask_logger.go
@@ -14,26 +14,26 @@ type MaskLogger struct {
RequestKey string
MaskRequest func(map[string]interface{})
MaskResponse func(map[string]interface{})
- StringFormat bool
+ JsonFormat bool
}
-func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool) *MaskLogger {
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat}
+func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger {
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat}
}
-func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
+func NewMaskLoggerWithSending(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *MaskLogger {
var keyMap map[string]string
if len(options) >= 1 {
keyMap = options[0]
}
- return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat, send: send, 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{}, includeRequest bool) {
if includeRequest && len(c.Request) > 0 {
- MaskRequest(c.Request, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(c.Request, fields, l.MaskRequest, l.JsonFormat)
}
- MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.StringFormat)
+ MaskResponse(ww, c, t1, response, fields, l.MaskResponse, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -41,7 +41,7 @@ func (l *MaskLogger) LogResponse(log func(context.Context, string, map[string]in
}
}
func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]interface{}), r *http.Request, fields map[string]interface{}) {
- MaskRequest(l.RequestKey, fields, l.MaskRequest, l.StringFormat)
+ MaskRequest(l.RequestKey, fields, l.MaskRequest, l.JsonFormat)
msg := "Request " + r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -49,7 +49,7 @@ func (l *MaskLogger) LogRequest(log func(context.Context, string, map[string]int
}
}
-func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(c.Response) > 0 {
fields[c.Response] = response
responseBody := response
@@ -57,15 +57,15 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
json.Unmarshal([]byte(responseBody), &responseMap)
if len(responseMap) > 0 {
mask(responseMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[c.Response] = responseMap
+ } else {
responseString, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[c.Response] = string(responseString)
}
- } else {
- fields[c.Response] = responseMap
}
}
}
@@ -82,7 +82,7 @@ func MaskResponse(ww WrapResponseWriter, c LogConfig, t1 time.Time, response str
}
}
-func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isStringFormat bool) {
+func MaskRequest(request string, fields map[string]interface{}, mask func(map[string]interface{}), isJsonFormat bool) {
if len(request) > 0 {
req, ok := fields[request]
if ok {
@@ -92,15 +92,15 @@ func MaskRequest(request string, fields map[string]interface{}, mask func(map[st
json.Unmarshal([]byte(requestBody), &requestMap)
if len(requestMap) > 0 {
mask(requestMap)
- if isStringFormat {
+ if isJsonFormat {
+ fields[request] = requestMap
+ } else {
requestString, err := json.Marshal(requestMap)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fields[request] = string(requestString)
}
- } else {
- fields[request] = requestMap
}
}
}
diff --git a/structured_logger.go b/structured_logger.go
index 1d54be4..afae636 100644
--- a/structured_logger.go
+++ b/structured_logger.go
@@ -14,10 +14,10 @@ type Formatter 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
- StringFormat bool
+ send func(context.Context, []byte, map[string]string) error
+ KeyMap map[string]string
+ RequestKey string
+ JsonFormat bool
}
var fieldConfig FieldConfig
@@ -25,20 +25,20 @@ var fieldConfig FieldConfig
func NewLogger() *StructuredLogger {
return &StructuredLogger{}
}
-func NewLoggerWithStringFormat(requestKey string, stringFormat bool) *StructuredLogger {
- return &StructuredLogger{RequestKey: requestKey, StringFormat: stringFormat}
+func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *StructuredLogger {
+ return &StructuredLogger{RequestKey: requestKey, JsonFormat: jsonFormat}
}
-func NewLoggerWithSending(requestKey string, stringFormat bool, send func(context.Context, []byte, map[string]string) error, options ...map[string]string) *StructuredLogger {
+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, StringFormat: stringFormat, send: send, KeyMap: keyMap}
+ 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.StringFormat)
+ BuildResponseBody(ww, c, t1, response, fields, l.JsonFormat)
msg := r.Method + " " + r.RequestURI
log(r.Context(), msg, fields)
if l.send != nil {
@@ -47,7 +47,7 @@ func (l *StructuredLogger) LogResponse(log func(context.Context, string, map[str
}
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.StringFormat && len(l.RequestKey) > 0 {
+ if l.JsonFormat && len(l.RequestKey) > 0 {
req, ok := fields[l.RequestKey]
if ok {
requestBody, ok2 := req.(string)
@@ -66,11 +66,9 @@ func (l *StructuredLogger) LogRequest(log func(context.Context, string, map[stri
}
}
-func BuildResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isStringFormat bool) {
+func BuildResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, response string, fields map[string]interface{}, isJsonFormat bool) {
if len(c.Response) > 0 {
- if isStringFormat {
- fields[c.Response] = response
- } else {
+ if isJsonFormat {
responseBody := response
responseMap := map[string]interface{}{}
json.Unmarshal([]byte(responseBody), &responseMap)
@@ -79,9 +77,11 @@ func BuildResponseBody(ww WrapResponseWriter, c LogConfig, t1 time.Time, respons
} else {
fields[c.Response] = response
}
+ } else {
+ fields[c.Response] = response
}
}
- if !isStringFormat && len(c.Request) > 0 {
+ if isJsonFormat && len(c.Request) > 0 {
req, ok := fields[c.Request]
if ok {
requestBody, ok2 := req.(string)