From a1a511fb9bd77dde0ad32c15e1f387beba4fd4dd Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Sat, 31 Aug 2024 00:12:32 +0700 Subject: [PATCH] Refactor code --- echo/config.go | 2 +- echo/mask_logger.go | 6 +++++- echo/structured_logger.go | 2 +- echo/v3/config.go | 2 +- echo/v3/mask_logger.go | 6 +++++- echo/v3/structured_logger.go | 2 +- gin/config.go | 2 +- gin/mask_logger.go | 6 +++++- gin/structured_logger.go | 2 +- log_config.go | 2 +- mask_logger.go | 6 +++++- structured_logger.go | 2 +- 12 files changed, 28 insertions(+), 12 deletions(-) diff --git a/echo/config.go b/echo/config.go index 68c16d4..fcb8951 100644 --- a/echo/config.go +++ b/echo/config.go @@ -3,7 +3,7 @@ package echo type LogConfig struct { Separate bool `yaml:"separate" mapstructure:"separate" json:"separate,omitempty" gorm:"column:separate" bson:"separate,omitempty" dynamodbav:"separate,omitempty" firestore:"separate,omitempty"` Build bool `yaml:"build" mapstructure:"build" json:"build,omitempty" gorm:"column:build" bson:"build,omitempty" dynamodbav:"build,omitempty" firestore:"build,omitempty"` - StringFormat bool `yaml:"string_format" mapstructure:"string_format" json:"stringFormat,omitempty" gorm:"column:string_format" bson:"stringFormat,omitempty" dynamodbav:"stringFormat,omitempty" firestore:"stringFormat,omitempty"` + Json bool `yaml:"json" mapstructure:"json" json:"json,omitempty" gorm:"column:json" bson:"json,omitempty" dynamodbav:"json,omitempty" firestore:"json,omitempty"` Log bool `yaml:"log" mapstructure:"log" json:"log,omitempty" gorm:"column:log" bson:"log,omitempty" dynamodbav:"log,omitempty" firestore:"log,omitempty"` Skips string `yaml:"skips" mapstructure:"skips" json:"skips,omitempty" gorm:"column:skips" bson:"skips,omitempty" dynamodbav:"skips,omitempty" firestore:"skips,omitempty"` Ip string `yaml:"ip" mapstructure:"ip" json:"ip,omitempty" gorm:"column:ip" bson:"ip,omitempty" dynamodbav:"ip,omitempty" firestore:"ip,omitempty"` diff --git a/echo/mask_logger.go b/echo/mask_logger.go index 3578cbc..bd62ca3 100644 --- a/echo/mask_logger.go +++ b/echo/mask_logger.go @@ -17,7 +17,11 @@ type MaskLogger struct { JsonFormat bool } -func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger { +func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), opts ...bool) *MaskLogger { + jsonFormat := false + if len(opts) > 0 { + jsonFormat = opts[0] + } return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat} } 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 { diff --git a/echo/structured_logger.go b/echo/structured_logger.go index 679df81..1e9f570 100644 --- a/echo/structured_logger.go +++ b/echo/structured_logger.go @@ -25,7 +25,7 @@ var fieldConfig FieldConfig func NewLogger() *StructuredLogger { return &StructuredLogger{} } -func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *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 { diff --git a/echo/v3/config.go b/echo/v3/config.go index 68c16d4..fcb8951 100644 --- a/echo/v3/config.go +++ b/echo/v3/config.go @@ -3,7 +3,7 @@ package echo type LogConfig struct { Separate bool `yaml:"separate" mapstructure:"separate" json:"separate,omitempty" gorm:"column:separate" bson:"separate,omitempty" dynamodbav:"separate,omitempty" firestore:"separate,omitempty"` Build bool `yaml:"build" mapstructure:"build" json:"build,omitempty" gorm:"column:build" bson:"build,omitempty" dynamodbav:"build,omitempty" firestore:"build,omitempty"` - StringFormat bool `yaml:"string_format" mapstructure:"string_format" json:"stringFormat,omitempty" gorm:"column:string_format" bson:"stringFormat,omitempty" dynamodbav:"stringFormat,omitempty" firestore:"stringFormat,omitempty"` + Json bool `yaml:"json" mapstructure:"json" json:"json,omitempty" gorm:"column:json" bson:"json,omitempty" dynamodbav:"json,omitempty" firestore:"json,omitempty"` Log bool `yaml:"log" mapstructure:"log" json:"log,omitempty" gorm:"column:log" bson:"log,omitempty" dynamodbav:"log,omitempty" firestore:"log,omitempty"` Skips string `yaml:"skips" mapstructure:"skips" json:"skips,omitempty" gorm:"column:skips" bson:"skips,omitempty" dynamodbav:"skips,omitempty" firestore:"skips,omitempty"` Ip string `yaml:"ip" mapstructure:"ip" json:"ip,omitempty" gorm:"column:ip" bson:"ip,omitempty" dynamodbav:"ip,omitempty" firestore:"ip,omitempty"` diff --git a/echo/v3/mask_logger.go b/echo/v3/mask_logger.go index 3578cbc..bd62ca3 100644 --- a/echo/v3/mask_logger.go +++ b/echo/v3/mask_logger.go @@ -17,7 +17,11 @@ type MaskLogger struct { JsonFormat bool } -func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger { +func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), opts ...bool) *MaskLogger { + jsonFormat := false + if len(opts) > 0 { + jsonFormat = opts[0] + } return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat} } 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 { diff --git a/echo/v3/structured_logger.go b/echo/v3/structured_logger.go index 679df81..1e9f570 100644 --- a/echo/v3/structured_logger.go +++ b/echo/v3/structured_logger.go @@ -25,7 +25,7 @@ var fieldConfig FieldConfig func NewLogger() *StructuredLogger { return &StructuredLogger{} } -func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *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 { diff --git a/gin/config.go b/gin/config.go index 7613213..5f6a91e 100644 --- a/gin/config.go +++ b/gin/config.go @@ -3,7 +3,7 @@ package gin type LogConfig struct { Separate bool `yaml:"separate" mapstructure:"separate" json:"separate,omitempty" gorm:"column:separate" bson:"separate,omitempty" dynamodbav:"separate,omitempty" firestore:"separate,omitempty"` Build bool `yaml:"build" mapstructure:"build" json:"build,omitempty" gorm:"column:build" bson:"build,omitempty" dynamodbav:"build,omitempty" firestore:"build,omitempty"` - StringFormat bool `yaml:"string_format" mapstructure:"string_format" json:"stringFormat,omitempty" gorm:"column:string_format" bson:"stringFormat,omitempty" dynamodbav:"stringFormat,omitempty" firestore:"stringFormat,omitempty"` + Json bool `yaml:"json" mapstructure:"json" json:"json,omitempty" gorm:"column:json" bson:"json,omitempty" dynamodbav:"json,omitempty" firestore:"json,omitempty"` Log bool `yaml:"log" mapstructure:"log" json:"log,omitempty" gorm:"column:log" bson:"log,omitempty" dynamodbav:"log,omitempty" firestore:"log,omitempty"` Skips string `yaml:"skips" mapstructure:"skips" json:"skips,omitempty" gorm:"column:skips" bson:"skips,omitempty" dynamodbav:"skips,omitempty" firestore:"skips,omitempty"` Ip string `yaml:"ip" mapstructure:"ip" json:"ip,omitempty" gorm:"column:ip" bson:"ip,omitempty" dynamodbav:"ip,omitempty" firestore:"ip,omitempty"` diff --git a/gin/mask_logger.go b/gin/mask_logger.go index c34d67d..b1e2787 100644 --- a/gin/mask_logger.go +++ b/gin/mask_logger.go @@ -17,7 +17,11 @@ type MaskLogger struct { JsonFormat bool } -func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger { +func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), opts ...bool) *MaskLogger { + jsonFormat := false + if len(opts) > 0 { + jsonFormat = opts[0] + } return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat} } 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 { diff --git a/gin/structured_logger.go b/gin/structured_logger.go index 8df8d73..ec50ac5 100644 --- a/gin/structured_logger.go +++ b/gin/structured_logger.go @@ -25,7 +25,7 @@ var fieldConfig FieldConfig func NewLogger() *StructuredLogger { return &StructuredLogger{} } -func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *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 { diff --git a/log_config.go b/log_config.go index cf73101..a225abf 100644 --- a/log_config.go +++ b/log_config.go @@ -3,7 +3,7 @@ package middleware type LogConfig struct { Separate bool `yaml:"separate" mapstructure:"separate" json:"separate,omitempty" gorm:"column:separate" bson:"separate,omitempty" dynamodbav:"separate,omitempty" firestore:"separate,omitempty"` Build bool `yaml:"build" mapstructure:"build" json:"build,omitempty" gorm:"column:build" bson:"build,omitempty" dynamodbav:"build,omitempty" firestore:"build,omitempty"` - StringFormat bool `yaml:"string_format" mapstructure:"string_format" json:"stringFormat,omitempty" gorm:"column:string_format" bson:"stringFormat,omitempty" dynamodbav:"stringFormat,omitempty" firestore:"stringFormat,omitempty"` + Json bool `yaml:"json" mapstructure:"json" json:"json,omitempty" gorm:"column:json" bson:"json,omitempty" dynamodbav:"json,omitempty" firestore:"json,omitempty"` Log bool `yaml:"log" mapstructure:"log" json:"log,omitempty" gorm:"column:log" bson:"log,omitempty" dynamodbav:"log,omitempty" firestore:"log,omitempty"` Skips string `yaml:"skips" mapstructure:"skips" json:"skips,omitempty" gorm:"column:skips" bson:"skips,omitempty" dynamodbav:"skips,omitempty" firestore:"skips,omitempty"` Ip string `yaml:"ip" mapstructure:"ip" json:"ip,omitempty" gorm:"column:ip" bson:"ip,omitempty" dynamodbav:"ip,omitempty" firestore:"ip,omitempty"` diff --git a/mask_logger.go b/mask_logger.go index 297b07e..b05af28 100644 --- a/mask_logger.go +++ b/mask_logger.go @@ -17,7 +17,11 @@ type MaskLogger struct { JsonFormat bool } -func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), jsonFormat bool) *MaskLogger { +func NewMaskLogger(requestKey string, maskRequest func(map[string]interface{}), maskResponse func(map[string]interface{}), opts ...bool) *MaskLogger { + jsonFormat := false + if len(opts) > 0 { + jsonFormat = opts[0] + } return &MaskLogger{RequestKey: requestKey, MaskRequest: maskRequest, MaskResponse: maskResponse, JsonFormat: jsonFormat} } 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 { diff --git a/structured_logger.go b/structured_logger.go index afae636..3fcdbc1 100644 --- a/structured_logger.go +++ b/structured_logger.go @@ -25,7 +25,7 @@ var fieldConfig FieldConfig func NewLogger() *StructuredLogger { return &StructuredLogger{} } -func NewLoggerWithStringFormat(requestKey string, jsonFormat bool) *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 {