1

I need to send zerolog logs to redis for a project and there seems to be an error in the way I am doing it.

I realize that in order to send a new output to zerolog, I need to send an io.Writer Type. So, I created a struct that has a specific io.Writer as shown below.

type RedisWriter struct {
    client *redis.ClusterClient
}
var redisWriter = RedisWriter{client: pub}

func (w RedisWriter) Write(p []byte) (n int, err error) {
    redisChannel := "COPS_LOGS"
    fmt.Println("blah")

    err = pub.Publish(ctx, redisChannel, p).Err()
    if err != nil {
        fmt.Printf("Error publishing message")
        return 0, err
    }
    return len(p), nil
}

func init() {
    
    logger = zerolog.New(redisWriter).With().Timestamp().Logger()
}

When I run the logger, I see the new Write method I created is never even being touched. When debugging through the code in zerolog, I see that the code is hitting the Write method in Zerolog. Do you know what I am doing wrong and what the fix is?

I also tried adding the redisWriter as a SyncWriter and as a MultiLevelWriter with the ConsoleWriter, but had no success.

  • Have you used the `logger` created here? If the issue is that the `Write` method is not called, it has nothing to do with redis. Try to reduce your demo to narrow down the issue. I just created a simple demo (https://go.dev/play/p/nqHJoMY38hC) and it works as expected. Try to modify this demo to reproduce your issue. – Zeke Lu Jul 14 '23 at 15:20
  • 1
    Thank you! This was really helpful even though I was doing the same thing. I was adding context to the same logger, but didn't provide it output again. That was the problem. – curlytech5000 Jul 14 '23 at 16:09

0 Answers0