Skip to content

Commit

Permalink
feat(option): add WithPathLevel option (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
goodevilgenius authored Feb 2, 2024
1 parent 9c92ccb commit 62dc492
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type config struct {
clientErrorLevel zerolog.Level
// the log level used for request with status code >= 500
serverErrorLevel zerolog.Level
// the log level to use for a specific path with status code < 400
pathLevels map[string]zerolog.Level
}

var isTerm bool = isatty.IsTerminal(os.Stdout.Fd())
Expand Down Expand Up @@ -117,6 +119,8 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
msg = c.Errors.String()
}

level, hasLevel := cfg.pathLevels[path]

switch {
case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError:
{
Expand All @@ -128,6 +132,11 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
l.WithLevel(cfg.serverErrorLevel).
Msg(msg)
}
case hasLevel:
{
l.WithLevel(level).
Msg(msg)
}
default:
l.WithLevel(cfg.defaultLevel).
Msg(msg)
Expand Down
38 changes: 38 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,44 @@ func TestLoggerParseLevel(t *testing.T) {
}
}

func TestLoggerCustomLevel(t *testing.T) {
buffer := new(bytes.Buffer)
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(SetLogger(
WithWriter(buffer),
WithDefaultLevel(zerolog.InfoLevel),
WithClientErrorLevel(zerolog.ErrorLevel),
WithServerErrorLevel(zerolog.FatalLevel),
WithPathLevel(map[string]zerolog.Level{
"/example": zerolog.DebugLevel,
}),
))
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")
})
r.GET("/example2", func(c *gin.Context) {})

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

buffer.Reset()
performRequest(r, "GET", "/example2")
assert.Contains(t, buffer.String(), "INF")

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

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

func BenchmarkLogger(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func WithSkipPath(s []string) Option {
})
}

// WithPathLevel use logging level for successful requests to a specific path
func WithPathLevel(m map[string]zerolog.Level) Option {
return optionFunc(func(c *config) {
c.pathLevels = m
})
}

// WithWriter change the default output writer.
// Default is gin.DefaultWriter
func WithWriter(s io.Writer) Option {
Expand Down

0 comments on commit 62dc492

Please sign in to comment.