Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(option): add WithPathLevel option #72

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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