Skip to content

Commit

Permalink
feat: logger support default fill
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jan 14, 2021
1 parent e259cb1 commit 39097a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
26 changes: 12 additions & 14 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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
}
Expand Down
22 changes: 3 additions & 19 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {<x-empty}",
OnLog: func(log string, _ *elton.Context) {
if log != "aslant.site 192.0.2.1:1234 192.0.2.1 GET / HTTP/1.1 a=1&b=2 HTTPS https://aslant.site/?a=1&b=2 https://aslant.site/ test-agent 13 13B 200 12 12B" {
t.Fatalf("log format fail")
}
assert.Equal("aslant.site 192.0.2.1:1234 192.0.2.1 GET / HTTP/1.1 a=1&b=2 HTTPS https://aslant.site/?a=1&b=2 https://aslant.site/ test-agent 13 13B 200 12 12B -", log)
},
}
m := NewLogger(config)
Expand Down Expand Up @@ -166,19 +165,4 @@ func TestLogger(t *testing.T) {
assert.Nil(err)
})

t.Run("get log function", func(t *testing.T) {
layout := "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human}"
fn := GenerateLog(layout)
req := httptest.NewRequest("GET", "https://aslant.site/?a=1&b=2", nil)
req.Header.Set("Referer", "https://aslant.site/")
req.Header.Set("User-Agent", "test-agent")
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.BodyBuffer = bytes.NewBufferString("response-body")
c.RequestBody = []byte("request-body")
c.StatusCode = 200
startedAt := time.Now()
assert.Equal("aslant.site 192.0.2.1:1234 192.0.2.1 GET / HTTP/1.1 a=1&b=2 HTTPS https://aslant.site/?a=1&b=2 https://aslant.site/ test-agent 13 13B 200 12 12B", fn(c, startedAt))
})

}

0 comments on commit 39097a7

Please sign in to comment.