我尝试用 WithOptions
添加钩子(Hook)但是没有打印任何内容来捕获一些日志事件:
logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
fmt.Println("test hooks test hooks")
return nil
}))
最佳答案
来自 documentation :
func (log *Logger) WithOptions(opts ...Option) *Logger
WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.
请注意,它克隆了一个新的记录器,而不是修改记录器。因此,您应该像这样重新分配记录器变量(或定义一个新变量):
logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
fmt.Println("test hooks test hooks")
return nil
}))
关于go - 如何将钩子(Hook)添加到 zap 记录器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64803634/