diff --git a/zap.go b/zap.go index 37dea94..6e8d748 100644 --- a/zap.go +++ b/zap.go @@ -18,6 +18,12 @@ import ( type Fn func(c *gin.Context) []zapcore.Field +// ZapLogger is the minimal logger interface compatible with zap.Logger +type ZapLogger interface { + Info(msg string, fields ...zap.Field) + Error(msg string, fields ...zap.Field) +} + // Config is config setting for Ginzap type Config struct { TimeFormat string @@ -34,12 +40,12 @@ type Config struct { // It receives: // 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 *zap.Logger, timeFormat string, utc bool) gin.HandlerFunc { +func Ginzap(logger ZapLogger, timeFormat string, utc bool) gin.HandlerFunc { return GinzapWithConfig(logger, &Config{TimeFormat: timeFormat, UTC: utc}) } // GinzapWithConfig returns a gin.HandlerFunc using configs -func GinzapWithConfig(logger *zap.Logger, conf *Config) gin.HandlerFunc { +func GinzapWithConfig(logger ZapLogger, conf *Config) gin.HandlerFunc { skipPaths := make(map[string]bool, len(conf.SkipPaths)) for _, path := range conf.SkipPaths { skipPaths[path] = true @@ -97,7 +103,7 @@ func defaultHandleRecovery(c *gin.Context, err interface{}) { // All errors are logged using zap.Error(). // stack means whether output the stack info. // The stack info is easy to find where the error occurs but the stack info is too large. -func RecoveryWithZap(logger *zap.Logger, stack bool) gin.HandlerFunc { +func RecoveryWithZap(logger ZapLogger, stack bool) gin.HandlerFunc { return CustomRecoveryWithZap(logger, stack, defaultHandleRecovery) } @@ -106,7 +112,7 @@ func RecoveryWithZap(logger *zap.Logger, stack bool) gin.HandlerFunc { // All errors are logged using zap.Error(). // stack means whether output the stack info. // The stack info is easy to find where the error occurs but the stack info is too large. -func CustomRecoveryWithZap(logger *zap.Logger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc { +func CustomRecoveryWithZap(logger ZapLogger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc { return func(c *gin.Context) { defer func() { if err := recover(); err != nil {