How do you create a basic database logger for uber-go.zap?

Viewed 666

I have been searching for examples of how to implement a database logger for uber-go.zip that will insert the message and fields into a database table for querying. (So no encoding) No luck so far finding some examples, so I am starting with the example from here:

How to access Fields in zap Hooks?

But the code doesnt even appear to do what it suggests it should do, here is my example:

https://go.dev/play/p/9AugEsYwA-E

package main

import (
    "errors"
    "fmt"

    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
)

func main() {
    fmt.Println("Hello, 世界")
    l, _ := zap.NewProduction()
    logger := zap.New(&CqlLogCore{Core: l.Core()})

    logger.Info("some logging info",
        zap.Int("count", 17),
        zap.Error(errors.New("boom")))

}

type CqlLogCore struct {
    zapcore.Core
}

func (c *CqlLogCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
    fmt.Println("hi", entry, fields)
    return c.Core.Write(entry, fields)
}

The test log message with "hi" is not dumped to stdout. Can someone please point me to where I am going wrong, or an example database logger.

Thanks!

0 Answers
Related