diff --git a/middleware/logger.go b/middleware/logger.go index 07b68c3..52ba978 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -88,9 +88,11 @@ type ( OnLog func(string, *elton.Context) // LoggerConfig logger config LoggerConfig struct { - Format string - OnLog OnLog - Skipper elton.Skipper + // DefaultFill default fill for empty value + DefaultFill string + Format string + OnLog OnLog + Skipper elton.Skipper } ) @@ -191,7 +193,7 @@ func parseLoggerTags(desc []byte) []*LoggerTag { } // formatLog 格式化访问日志信息 -func formatLog(c *elton.Context, tags []*LoggerTag, startedAt time.Time) string { +func formatLog(c *elton.Context, tags []*LoggerTag, startedAt time.Time, defaultFill string) string { fn := func(tag *LoggerTag) string { switch tag.category { case host: @@ -277,19 +279,15 @@ func formatLog(c *elton.Context, tags []*LoggerTag, startedAt time.Time) string arr := make([]string, 0, len(tags)) for _, tag := range tags { - arr = append(arr, fn(tag)) + v := fn(tag) + if v == "" { + v = defaultFill + } + arr = append(arr, v) } return strings.Join(arr, "") } -// GenerateLog generate log function -func GenerateLog(layout string) func(*elton.Context, time.Time) string { - tags := parseLoggerTags([]byte(layout)) - return func(c *elton.Context, startedAt time.Time) string { - return formatLog(c, tags, startedAt) - } -} - // New create a logger middleware func NewLogger(config LoggerConfig) elton.Handler { if config.Format == "" { @@ -309,7 +307,7 @@ func NewLogger(config LoggerConfig) elton.Handler { } startedAt := time.Now() err = c.Next() - str := formatLog(c, tags, startedAt) + str := formatLog(c, tags, startedAt, config.DefaultFill) config.OnLog(str, c) return err } diff --git a/middleware/logger_test.go b/middleware/logger_test.go index a5cdef4..894af80 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -47,11 +47,10 @@ func TestLogger(t *testing.T) { assert := assert.New(t) t.Run("normal", func(t *testing.T) { config := LoggerConfig{ - Format: "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human}", + DefaultFill: "-", + Format: "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human} {