Skip to content

Commit

Permalink
feat: make log levels customizable (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Giulio Ruggeri <[email protected]>
  • Loading branch information
dellekappa and Giulio Ruggeri authored Feb 20, 2022
1 parent 20037e5 commit bec61d6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func main() {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Example custom log level.
r.GET("/debugonly", logger.SetLogger(
logger.WithDefaultLevel(zerolog.DebugLevel),
), func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

r.GET("/id", requestid.New(requestid.Config{
Generator: func() string {
return "foo-bar"
Expand Down
37 changes: 32 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type config struct {
// Output is a writer where logs are written.
// Optional. Default value is gin.DefaultWriter.
output io.Writer
// the log level used for request with status code < 400
defaultLevel zerolog.Level
// the log level used for request with status code between 400 and 499
clientErrorLevel zerolog.Level
// the log level used for request with status code >= 500
serverErrorLevel zerolog.Level
}

// WithLogger set custom logger func
Expand Down Expand Up @@ -85,11 +91,32 @@ func WithWriter(s io.Writer) Option {
}
}

func WithDefaultLevel(lvl zerolog.Level) Option {
return func(c *config) {
c.defaultLevel = lvl
}
}

func WithClientErrorLevel(lvl zerolog.Level) Option {
return func(c *config) {
c.clientErrorLevel = lvl
}
}

func WithServerErrorLevel(lvl zerolog.Level) Option {
return func(c *config) {
c.serverErrorLevel = lvl
}
}

// SetLogger initializes the logging middleware.
func SetLogger(opts ...Option) gin.HandlerFunc {
l := &config{
logger: defaultLogger,
output: gin.DefaultWriter,
logger: defaultLogger,
defaultLevel: zerolog.InfoLevel,
clientErrorLevel: zerolog.WarnLevel,
serverErrorLevel: zerolog.ErrorLevel,
output: gin.DefaultWriter,
}

// Loop through each option
Expand Down Expand Up @@ -143,16 +170,16 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
switch {
case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError:
{
logger.Warn().
logger.WithLevel(l.clientErrorLevel).
Msg(msg)
}
case c.Writer.Status() >= http.StatusInternalServerError:
{
logger.Error().
logger.WithLevel(l.serverErrorLevel).
Msg(msg)
}
default:
logger.Info().
logger.WithLevel(l.defaultLevel).
Msg(msg)
}
}
Expand Down
30 changes: 30 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,33 @@ func TestLoggerWithLogger(t *testing.T) {
performRequest(r, "GET", "/regexp02")
assert.NotContains(t, buffer.String(), "/regexp02")
}

func TestLoggerWithLevels(t *testing.T) {
buffer := new(bytes.Buffer)
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(SetLogger(
WithWriter(buffer),
WithDefaultLevel(zerolog.DebugLevel),
WithClientErrorLevel(zerolog.ErrorLevel),
WithServerErrorLevel(zerolog.FatalLevel),
))
r.GET("/example", func(c *gin.Context) {})
r.POST("/example", func(c *gin.Context) {
c.String(http.StatusBadRequest, "ok")
})
r.PUT("/example", func(c *gin.Context) {
c.String(http.StatusBadGateway, "ok")
})

performRequest(r, "GET", "/example?a=100")
assert.Contains(t, buffer.String(), "DBG")

buffer.Reset()
performRequest(r, "POST", "/example?a=100")
assert.Contains(t, buffer.String(), "ERR")

buffer.Reset()
performRequest(r, "PUT", "/example?a=100")
assert.Contains(t, buffer.String(), "FTL")
}

0 comments on commit bec61d6

Please sign in to comment.