Why is this zerolog hook an ineffective assignment?

Issue

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)

Solution

Thanks to the help I got it working:

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

Answered By – Steffen

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.