Skip to content

Commit

Permalink
feat: refactor logging functions to use ZapLogger interface
Browse files Browse the repository at this point in the history
- 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 #39
fix #38

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Sep 7, 2023
1 parent 4fa00f2 commit 4622367
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 {
Expand Down

0 comments on commit 4622367

Please sign in to comment.