From 462236747658c59fc2d9d55b52e38eeac6c9c5c7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 7 Sep 2023 23:49:52 +0800 Subject: [PATCH] feat: refactor logging functions to use `ZapLogger` interface - Add a new interface `ZapLogger` with `Info` and `Error` methods - Change the type of the `logger` parameter in the `Ginzap` function to `ZapLogger` - Change the type of the `logger` parameter in the `GinzapWithConfig` function to `ZapLogger` - Change the type of the `logger` parameter in the `RecoveryWithZap` function to `ZapLogger` - Change the type of the `logger` parameter in the `CustomRecoveryWithZap` function to `ZapLogger` co-author: @a5r0n https://github.com/a5r0n fix https://github.com/gin-contrib/zap/pull/39 fix https://github.com/gin-contrib/zap/issues/38 Signed-off-by: Bo-Yi Wu --- zap.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 {