diff --git a/zap.go b/zap.go index 6e8d748..60ef297 100644 --- a/zap.go +++ b/zap.go @@ -26,10 +26,11 @@ type ZapLogger interface { // Config is config setting for Ginzap type Config struct { - TimeFormat string - UTC bool - SkipPaths []string - Context Fn + TimeFormat string + UTC bool + SkipPaths []string + Context Fn + DefaultLevel zapcore.Level } // Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap. @@ -41,7 +42,7 @@ type Config struct { // 1. A time package format string (e.g. time.RFC3339). // 2. A boolean stating whether to use UTC time zone or local. func Ginzap(logger ZapLogger, timeFormat string, utc bool) gin.HandlerFunc { - return GinzapWithConfig(logger, &Config{TimeFormat: timeFormat, UTC: utc}) + return GinzapWithConfig(logger, &Config{TimeFormat: timeFormat, UTC: utc, DefaultLevel: zapcore.InfoLevel}) } // GinzapWithConfig returns a gin.HandlerFunc using configs @@ -88,7 +89,12 @@ func GinzapWithConfig(logger ZapLogger, conf *Config) gin.HandlerFunc { logger.Error(e, fields...) } } else { - logger.Info(path, fields...) + zl, ok := logger.(*zap.Logger) + if ok { + zl.Log(conf.DefaultLevel, path, fields...) + } else { + logger.Error(path, fields...) + } } } } diff --git a/zap_test.go b/zap_test.go index 46b702f..70e4ff7 100644 --- a/zap_test.go +++ b/zap_test.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "go.uber.org/zap" + "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" ) @@ -83,9 +84,10 @@ func TestGinzapWithConfig(t *testing.T) { utcLogger, utcLoggerObserved := buildDummyLogger() r.Use(GinzapWithConfig(utcLogger, &Config{ - TimeFormat: time.RFC3339, - UTC: true, - SkipPaths: []string{"/no_log"}, + TimeFormat: time.RFC3339, + UTC: true, + SkipPaths: []string{"/no_log"}, + DefaultLevel: zapcore.WarnLevel, })) r.GET("/test", func(c *gin.Context) { @@ -122,4 +124,8 @@ func TestGinzapWithConfig(t *testing.T) { if err != nil { t.Fatal(err) } + + if logLine.Level != zapcore.WarnLevel { + t.Fatalf("log level should be warn but was %s", logLine.Level.String()) + } }