Home > Enterprise >  Why is this zerolog hook an ineffective assignment?
Why is this zerolog hook an ineffective assignment?

Time:02-28

I want to store zerolog messages in a slice for easy access to the last n messages. memoryLog.messages stays null and golangci-lint complains about ineffective assignment to field memoryLog.messages. I'm still new to Go and it feels like I'm missing something obvious, but I just can't figure it out.

type memoryLog struct {
    config   config
    messages []string
}

func (memoryLog memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
    memoryLog.messages = append([]string{message}, memoryLog.messages...)

    if len(memoryLog.messages) > memoryLog.config.MemoryLogMaxItems {
        memoryLog.messages = memoryLog.messages[:memoryLog.config.MemoryLogMaxItems]
    }
}
memoryLog := memoryLog{config: config}
logger := log.Hook(memoryLog)

CodePudding user response:

Thanks to the help I got it working:

func (log *memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
    //...
}
logger := log.Hook(&memoryLog)
  •  Tags:  
  • go
  • Related