Skip to content

Commit

Permalink
fix #1 Add new import go.uber.org/zap
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 18, 2017
1 parent 8b56671 commit 4ba0a83
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin)

Alternative logging through [zap](https://github.com/uber-go/zap). Thanks for [Pull Request](https://github.com/gin-gonic/contrib/pull/129) from [@yezooz](https://github.com/yezooz)

## Example

See the [example](example/main.go).
Expand Down
9 changes: 3 additions & 6 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import (

"github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/uber-go/zap"
"go.uber.org/zap"
)

func main() {
r := gin.New()

log := zap.New(
zap.NewTextEncoder(),
zap.DebugLevel,
)
logger, _ := zap.NewProduction()

// Add a ginzap middleware, which:
// - Logs all requests, like a combined access and error log.
// - Logs to stdout.
// - RFC3339 with UTC time format.
r.Use(ginzap.Ginzap(log, time.RFC3339, true))
r.Use(ginzap.Ginzap(logger, time.RFC3339, true))

// Example ping request.
r.GET("/ping", func(c *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/uber-go/zap"
"go.uber.org/zap"
)

// Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.
Expand All @@ -17,7 +17,7 @@ import (
// It receives:
// 1. A time package format string (e.g. time.RFC3339).
// 2. A boolean stating whether to use UTC time zone or local.
func Ginzap(logger zap.Logger, timeFormat string, utc bool) gin.HandlerFunc {
func Ginzap(logger *zap.Logger, timeFormat string, utc bool) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
Expand Down

0 comments on commit 4ba0a83

Please sign in to comment.