:bug:修复gin显示header异常

This commit is contained in:
comma
2025-10-29 18:31:24 +08:00
parent 54efe7547a
commit 9a521164fc

View File

@@ -29,13 +29,13 @@ type FormatFunc func(logParam LogParam) string
type Skipper func(c *gin.Context) bool type Skipper func(c *gin.Context) bool
var defaultConfig = &Config{ var defaultConfig = &GinLogConfig{
TimeFormat: "2006-01-02 15:04:05.000", TimeFormat: "2006-01-02 15:04:05.000",
SkipPaths: nil, SkipPaths: nil,
SkipPathRegexps: nil, SkipPathRegexps: nil,
DefaultLevel: zapcore.InfoLevel, DefaultLevel: zapcore.InfoLevel,
Skipper: nil, Skipper: nil,
formatFunc: defaultFormatFunc, FormatFunc: defaultFormatFunc,
HideKeys: []string{"header"}, HideKeys: []string{"header"},
} }
@@ -67,13 +67,13 @@ var defaultFormatFunc FormatFunc = func(logParam LogParam) string {
return builder.String() return builder.String()
} }
type Config struct { type GinLogConfig struct {
TimeFormat string // custom time format default: 2006-01-02 15:04:05.000 TimeFormat string // custom time format default: 2006-01-02 15:04:05.000
SkipPaths []string // skip path SkipPaths []string // skip path
SkipPathRegexps []*regexp.Regexp SkipPathRegexps []*regexp.Regexp
DefaultLevel zapcore.Level DefaultLevel zapcore.Level
Skipper Skipper Skipper Skipper
formatFunc FormatFunc FormatFunc FormatFunc
HideKeys []string // current only use 'header' 'errors' HideKeys []string // current only use 'header' 'errors'
} }
@@ -83,11 +83,11 @@ func GinZap() gin.HandlerFunc {
func GinZapWithFormat(formatFunc FormatFunc) gin.HandlerFunc { func GinZapWithFormat(formatFunc FormatFunc) gin.HandlerFunc {
conf := defaultConfig conf := defaultConfig
conf.formatFunc = formatFunc conf.FormatFunc = formatFunc
return GinZapWithConfig(conf) return GinZapWithConfig(conf)
} }
func GinZapWithConfig(conf *Config) gin.HandlerFunc { func GinZapWithConfig(conf *GinLogConfig) gin.HandlerFunc {
skipPaths := make(map[string]bool, len(conf.SkipPaths)) skipPaths := make(map[string]bool, len(conf.SkipPaths))
for _, path := range conf.SkipPaths { for _, path := range conf.SkipPaths {
skipPaths[path] = true skipPaths[path] = true
@@ -96,8 +96,8 @@ func GinZapWithConfig(conf *Config) gin.HandlerFunc {
conf.TimeFormat = "2006-01-02 15:04:05.000" conf.TimeFormat = "2006-01-02 15:04:05.000"
} }
if conf.formatFunc == nil { if conf.FormatFunc == nil {
conf.formatFunc = defaultFormatFunc conf.FormatFunc = defaultFormatFunc
} }
return func(c *gin.Context) { return func(c *gin.Context) {
@@ -138,7 +138,7 @@ func GinZapWithConfig(conf *Config) gin.HandlerFunc {
} }
if !slices.Contains(conf.HideKeys, "header") { if !slices.Contains(conf.HideKeys, "header") {
headerJson, _ := json.Marshal(logParam.Header) headerJson, _ := json.Marshal(c.Request.Header)
logParam.Header = string(headerJson) logParam.Header = string(headerJson)
} }
if !slices.Contains(conf.HideKeys, "error") { if !slices.Contains(conf.HideKeys, "error") {
@@ -150,9 +150,9 @@ func GinZapWithConfig(conf *Config) gin.HandlerFunc {
} }
if len(c.Errors.Errors()) > 0 { if len(c.Errors.Errors()) > 0 {
log.Error(conf.formatFunc(logParam)) log.Error(conf.FormatFunc(logParam))
} else { } else {
log.Info(conf.formatFunc(logParam)) log.Info(conf.FormatFunc(logParam))
} }
} }
} }