How do I write golang zerologs to Redis?

110 Views Asked by At

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.

0

There are 0 best solutions below