关于golang:聊聊golang的zap的hook

40次阅读

共计 2131 个字符,预计需要花费 6 分钟才能阅读完成。

本文次要钻研一下 golang 的 zap 的 hook

实例

func hookDemo() {count := &atomic.Int64{}
    logger, _ := zap.NewProduction(zap.Hooks(func(entry zapcore.Entry) error {fmt.Println("count:", count.Inc(), "msg:", entry.Message)
        return nil
    }))
    defer logger.Sync() // flushes buffer, if any
    sugar := logger.Sugar()
    sugar.Infow("failed to fetch URL",
        // Structured context as loosely typed key-value pairs.
        "url", "https://golang.org",
        "attempt", 3,
        "backoff", time.Second,
    )
    sugar.Info("hello world")
}

输入

{"level":"info","ts":1608045721.769727,"caller":"zap/zap_demo.go:29","msg":"failed to fetch URL","url":"https://golang.org","attempt":3,"backoff":1}
count: 1 msg: failed to fetch URL
{"level":"info","ts":1608045721.769826,"caller":"zap/zap_demo.go:35","msg":"hello world"}
count: 2 msg: hello world

Hooks

zap@v1.16.0/options.go

func Hooks(hooks ...func(zapcore.Entry) error) Option {return optionFunc(func(log *Logger) {log.core = zapcore.RegisterHooks(log.core, hooks...)
    })
}

Hooks 办法将 log 的 core 应用 zapcore.RegisterHooks 包装了一下

zapcore.RegisterHooks

zap@v1.16.0/zapcore/hook.go

func RegisterHooks(core Core, hooks ...func(Entry) error) Core {funcs := append([]func(Entry) error{}, hooks...)
    return &hooked{
        Core:  core,
        funcs: funcs,
    }
}

RegisterHooks 办法创立 hooked,hooks 赋值给 hooked 的 funcs 属性

hook

zap@v1.16.0/zapcore/hook.go

type hooked struct {
    Core
    funcs []func(Entry) error
}

func (h *hooked) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
    // Let the wrapped Core decide whether to log this message or not. This
    // also gives the downstream a chance to register itself directly with the
    // CheckedEntry.
    if downstream := h.Core.Check(ent, ce); downstream != nil {return downstream.AddCore(ent, h)
    }
    return ce
}

func (h *hooked) With(fields []Field) Core {
    return &hooked{Core:  h.Core.With(fields),
        funcs: h.funcs,
    }
}

func (h *hooked) Write(ent Entry, _ []Field) error {
    // Since our downstream had a chance to register itself directly with the
    // CheckedMessage, we don't need to call it here.
    var err error
    for i := range h.funcs {err = multierr.Append(err, h.funcs[i](ent))
    }
    return err
}

hooked 内嵌了 Core,它笼罩了 Check、With、Write 办法;Check 办法将 hooked 增加到 downstream;Write 办法遍历 hooks,执行回调

小结

Hooks 办法将 log 的 core 应用 zapcore.RegisterHooks 包装了一下;RegisterHooks 办法创立 hooked,hooks 赋值给 hooked 的 funcs 属性;hooked 包装了 core,因此须要在 Check 的时候把本人注册进去,而后在 Write 的时候就能够执行到本人注册的 hooks。个别能够将 metrics 等简略的操作通过 hook 来实现,而简单的逻辑则最好通过实现 zapcore.Core 来做。

doc

  • zap

正文完
 0