diff --git a/README.md b/README.md index 3568f3e..6d21b09 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# cod-logger +# elton-logger -[![Build Status](https://img.shields.io/travis/vicanso/cod-logger.svg?label=linux+build)](https://travis-ci.org/vicanso/cod-logger) +[![Build Status](https://img.shields.io/travis/vicanso/elton-logger.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-logger) -Access logger for cod, it support multiple tags from request and response. +Access logger for elton, it support multiple tags from request and response. ```go package main @@ -11,25 +11,25 @@ import ( "bytes" "fmt" - "github.com/vicanso/cod" + "github.com/vicanso/elton" - codlogger "github.com/vicanso/cod-logger" + codlogger "github.com/vicanso/elton-logger" ) func main() { - d := cod.New() + d := elton.New() d.Use(codlogger.New(codlogger.Config{ Format: codlogger.CommonFormat, - OnLog: func(str string, _ *cod.Context) { + OnLog: func(str string, _ *elton.Context) { fmt.Println(str) }, })) // http://127.0.0.1:7001/?_fields=foo,id - d.GET("/", func(c *cod.Context) (err error) { + d.GET("/", func(c *elton.Context) (err error) { c.StatusCode = 200 - c.SetHeader(cod.HeaderContentType, cod.MIMEApplicationJSON) + c.SetHeader(elton.HeaderContentType, elton.MIMEApplicationJSON) c.BodyBuffer = bytes.NewBufferString(`{ "foo": "bar", "id": 1, diff --git a/go.mod b/go.mod index d3b419c..566803a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module github.com/vicanso/cod-logger +module github.com/vicanso/elton-logger -require github.com/vicanso/cod v0.0.2 +go 1.12 + +require github.com/vicanso/elton v0.2.0 diff --git a/go.sum b/go.sum index 4b8ead7..c530797 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/vicanso/cod v0.0.2 h1:bTGMY9Qp4R+rsgFXXSexmt3GSAcxL/zyxRhRfaSlRcQ= -github.com/vicanso/cod v0.0.2/go.mod h1:m+avmqlCzw472clYqSNtyNNm+BO3DW9OJCek23kSUUE= -github.com/vicanso/hes v0.1.3 h1:3txPJL+J+xBRhIotkK3XKNbmBz+JXJ1knmGM0L+Qe4k= -github.com/vicanso/hes v0.1.3/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/vicanso/elton v0.2.0 h1:QlXgmq6m+9wZN7FeLD25/EhBkl8blzppviVY5U5PTm0= +github.com/vicanso/elton v0.2.0/go.mod h1:ynAUOSkZQ+pFaUsxlG5hYnJFjPpMwz8YyEBPzNh0pSg= +github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw= +github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo= github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= github.com/vicanso/keygrip v0.1.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho= diff --git a/logger.go b/logger.go index f668a36..9f21b65 100644 --- a/logger.go +++ b/logger.go @@ -22,7 +22,7 @@ import ( "time" "unsafe" - "github.com/vicanso/cod" + "github.com/vicanso/elton" ) const ( @@ -70,12 +70,12 @@ type ( data string } // OnLog on log function - OnLog func(string, *cod.Context) + OnLog func(string, *elton.Context) // Config logger config Config struct { Format string OnLog OnLog - Skipper cod.Skipper + Skipper elton.Skipper } ) @@ -171,7 +171,7 @@ func parse(desc []byte) []*Tag { } // format 格式化访问日志信息 -func format(c *cod.Context, tags []*Tag, startedAt time.Time) string { +func format(c *elton.Context, tags []*Tag, startedAt time.Time) string { fn := func(tag *Tag) string { switch tag.category { case host: @@ -261,15 +261,15 @@ func format(c *cod.Context, tags []*Tag, startedAt time.Time) string { } // GenerateLog generate log function -func GenerateLog(layout string) func(*cod.Context, time.Time) string { +func GenerateLog(layout string) func(*elton.Context, time.Time) string { tags := parse([]byte(layout)) - return func(c *cod.Context, startedAt time.Time) string { + return func(c *elton.Context, startedAt time.Time) string { return format(c, tags, startedAt) } } // New create a logger middleware -func New(config Config) cod.Handler { +func New(config Config) elton.Handler { if config.Format == "" { panic("logger require format") } @@ -279,9 +279,9 @@ func New(config Config) cod.Handler { tags := parse([]byte(config.Format)) skipper := config.Skipper if skipper == nil { - skipper = cod.DefaultSkipper + skipper = elton.DefaultSkipper } - return func(c *cod.Context) (err error) { + return func(c *elton.Context) (err error) { if skipper(c) { return c.Next() } diff --git a/logger_test.go b/logger_test.go index ab3ed6e..0a8acef 100644 --- a/logger_test.go +++ b/logger_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/vicanso/cod" + "github.com/vicanso/elton" ) func TestGetHumanReadableSize(t *testing.T) { @@ -35,7 +35,7 @@ func TestLogger(t *testing.T) { config := Config{ Format: "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human}", - OnLog: func(log string, _ *cod.Context) { + 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") } @@ -46,7 +46,7 @@ func TestLogger(t *testing.T) { req.Header.Set("Referer", "https://aslant.site/") req.Header.Set("User-Agent", "test-agent") resp := httptest.NewRecorder() - c := cod.NewContext(resp, req) + c := elton.NewContext(resp, req) c.BodyBuffer = bytes.NewBufferString("response-body") c.RequestBody = []byte("request-body") c.StatusCode = 200 @@ -59,7 +59,7 @@ func TestLogger(t *testing.T) { t.Run("latency", func(t *testing.T) { config := Config{ Format: "{latency} {latency-ms}", - OnLog: func(log string, _ *cod.Context) { + OnLog: func(log string, _ *elton.Context) { if len(strings.Split(log, " ")) != 2 { t.Fatalf("get latency fail") } @@ -68,7 +68,7 @@ func TestLogger(t *testing.T) { m := New(config) req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil) resp := httptest.NewRecorder() - c := cod.NewContext(resp, req) + c := elton.NewContext(resp, req) c.Next = func() error { time.Sleep(time.Second) return nil @@ -79,7 +79,7 @@ func TestLogger(t *testing.T) { t.Run("when", func(t *testing.T) { config := Config{ Format: "{when} {when-iso} {when-utc-iso} {when-unix} {when-iso-ms} {when-utc-iso-ms}", - OnLog: func(log string, _ *cod.Context) { + OnLog: func(log string, _ *elton.Context) { if len(strings.Split(log, " ")) != 6 { t.Fatalf("get when fail") } @@ -88,7 +88,7 @@ func TestLogger(t *testing.T) { m := New(config) req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil) resp := httptest.NewRecorder() - c := cod.NewContext(resp, req) + c := elton.NewContext(resp, req) c.Next = func() error { return nil } @@ -98,7 +98,7 @@ func TestLogger(t *testing.T) { t.Run("cookie", func(t *testing.T) { config := Config{ Format: "{~jt}", - OnLog: func(log string, _ *cod.Context) { + OnLog: func(log string, _ *elton.Context) { if log != "abc" { t.Fatalf("get cookie value fail") } @@ -111,7 +111,7 @@ func TestLogger(t *testing.T) { Value: "abc", }) resp := httptest.NewRecorder() - c := cod.NewContext(resp, req) + c := elton.NewContext(resp, req) c.Next = func() error { return nil } @@ -121,7 +121,7 @@ func TestLogger(t *testing.T) { t.Run("header", func(t *testing.T) { config := Config{ Format: "{>X-Token} {