如何在 uber-zap 的钩子中访问有关日志记录事件的完整信息?
例如,我正在尝试向zapcore.Field
日志记录事件添加一个,但它没有显示在zapcore.Entry
.
如果不可能,我至少可以以某种方式获得完全格式化的字符串吗?目标是在出现错误时发送电子邮件/自动消息/哨兵/等。
package main
import (
"log"
"github.com/davecgh/go-spew/spew"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
prodLogger, err := zap.NewProduction(zap.Hooks(func(entry zapcore.Entry) error {
if entry.Level == zapcore.ErrorLevel {
spew.Dump(entry) // fancy console printer
}
return nil
}))
if err != nil {
log.Fatal(err)
}
prodLogger.
Named("logger_name").
Error("something happened", zap.String("foo", "bar"))
}
foo
输出 - 没有or的痕迹bar
:
{"level":"error","ts":1640722252.899601,"logger":"logger_name","caller":"awesomep2/main.go:23","msg":"something happened","foo":"bar","stacktrace":"main.main\n\t/Users/xxx/GitHub/awesomep2/main.go:23\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:255"}
(zapcore.Entry) {
Level: (zapcore.Level) error,
Time: (time.Time) 2021-12-28 13:10:52.899601 -0700 MST m=+0.000629089,
LoggerName: (string) (len=11) "logger_name",
Message: (string) (len=18) "something happened",
Caller: (zapcore.EntryCaller) /Users/xxx/GitHub/awesomep2/main.go:23,
Stack: (string) (len=103) "main.main\n\t/Users/xxx/GitHub/awesomep2/main.go:23\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:255"
}