diff --git a/README.md b/README.md index a4f8e46..3ea7e5d 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/logger.go b/logger.go index 1c6e3cd..775e952 100644 --- a/logger.go +++ b/logger.go @@ -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 @@ -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 @@ -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) } } diff --git a/logger_test.go b/logger_test.go index a6d7253..5983170 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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") +}