Skip to content

Commit

Permalink
feat: change log format
Browse files Browse the repository at this point in the history
  • Loading branch information
brpaz committed Sep 30, 2019
1 parent 79a101d commit 7dfd3d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ engines:
golint:
govet:
coverage:
#exclude_paths:
# - config/engines.yml
exclude_paths:
- 'vendor/**'
- 'testdata/**/*'
- 'README.md'
19 changes: 10 additions & 9 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package echozap

import (
"fmt"
"time"

"github.com/labstack/echo/v4"
Expand All @@ -22,20 +23,20 @@ func ZapLogger(log *zap.Logger) echo.MiddlewareFunc {
req := c.Request()
res := c.Response()

id := req.Header.Get(echo.HeaderXRequestID)
if id == "" {
id = res.Header().Get(echo.HeaderXRequestID)
}

fields := []zapcore.Field{
zap.String("remote_ip", c.RealIP()),
zap.String("time", time.Since(start).String()),
zap.String("request_id", id),
zap.String("host", req.Host),
zap.String("method", req.Method),
zap.String("uri", req.RequestURI),
zap.String("request", fmt.Sprintf("%s %s", req.Method, req.RequestURI)),
zap.Int("status", res.Status),
zap.String("size", res.Header().Get("Content-Length")),
zap.Int64("size", res.Size),
zap.String("user_agent", req.UserAgent()),
}

id := req.Header.Get(echo.HeaderXRequestID)
if id == "" {
id = res.Header().Get(echo.HeaderXRequestID)
fields = append(fields, zap.String("request_id", id))
}

n := res.Status
Expand Down
3 changes: 1 addition & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func TestZapLogger(t *testing.T) {
assert.Equal(t, 1, logs.Len())
assert.Equal(t, int64(200), logFields["status"])
assert.NotNil(t, logFields["time"])
assert.Equal(t, "GET", logFields["method"])
assert.Equal(t, "/something", logFields["uri"])
assert.Equal(t, "GET /something", logFields["request"])
assert.NotNil(t, logFields["host"])
assert.NotNil(t, logFields["size"])
}

0 comments on commit 7dfd3d6

Please sign in to comment.