1

I try to add hook with WithOptions but there was nothing printed for catching some of log events:

    logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
        fmt.Println("test hooks test hooks")
        return nil
    }))
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Viktor
  • 1,532
  • 6
  • 22
  • 61

1 Answers1

5

From the 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.

Notice that it clones a new logger instead of modifying the logger. So, you should reassign the logger variable (or define a new variable) like this:

logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
    fmt.Println("test hooks test hooks")
    return nil
}))
wijayaerick
  • 703
  • 6
  • 12