Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Commit

Permalink
refactor: update module
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 15, 2019
1 parent 19faf09 commit a3412dd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
18 changes: 9 additions & 9 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"time"
"unsafe"

"github.com/vicanso/cod"
"github.com/vicanso/elton"
)

const (
Expand Down Expand Up @@ -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
}
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
}
Expand All @@ -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()
}
Expand Down
24 changes: 12 additions & 12 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/vicanso/cod"
"github.com/vicanso/elton"
)

func TestGetHumanReadableSize(t *testing.T) {
Expand All @@ -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")
}
Expand All @@ -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
Expand All @@ -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")
}
Expand All @@ -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
Expand All @@ -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")
}
Expand All @@ -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
}
Expand All @@ -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")
}
Expand All @@ -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
}
Expand All @@ -121,7 +121,7 @@ func TestLogger(t *testing.T) {
t.Run("header", func(t *testing.T) {
config := Config{
Format: "{>X-Token} {<X-Response-Id} place-holder",
OnLog: func(log string, _ *cod.Context) {
OnLog: func(log string, _ *elton.Context) {
if log != "abc def place-holder" {
t.Fatalf("get header value fail")
}
Expand All @@ -131,7 +131,7 @@ func TestLogger(t *testing.T) {
req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil)
req.Header.Set("X-Token", "abc")
resp := httptest.NewRecorder()
c := cod.NewContext(resp, req)
c := elton.NewContext(resp, req)
c.SetHeader("X-Response-Id", "def")
c.Next = func() error {
return nil
Expand All @@ -146,7 +146,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
Expand Down

0 comments on commit a3412dd

Please sign in to comment.