How can I access the full information about the logging event in uber-zap's hooks?
For example, I am trying to add a zapcore.Field
to the logging event, but it does not show up in the zapcore.Entry
.
If it is not possible, can I at least have the fully-formatted string somehow? The goal is to send an email/automated message/Sentry/etc in case of errors.
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"))
}
Output - no traces of 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"
}