Download and install it:
go get github.com/lixvyang/ginzero
Import it in your code:
import "github.com/lixvyang/ginzero"
See the example.
package main
import (
"os"
"github.com/gin-gonic/gin"
"github.com/lixvyang/ginzero"
"github.com/rs/zerolog"
)
func NewLogger() zerolog.Logger {
logger := zerolog.New(os.Stdout).
With().
Caller().
Timestamp().
Logger()
return logger
}
func main() {
logger := NewLogger()
r := gin.New()
r.Use(ginzero.Ginzero(&logger), ginzero.RecoveryWithZero(&logger, true))
r.GET("/hello", func(c *gin.Context) {
c.String(200, "hello")
})
r.GET("/panic", func(c *gin.Context) {
panic("panic msg.")
})
r.Run(":8002")
}
When you want to skip logging for specific path, please use GinzapWithConfig
r.Use(ginzero.Ginzero(&logger, ginzero.WithSkipPaths([]string{"/hello"})))