Is there a way to use Zap structured logger in Go and export it to be called in a C script using cgo?

258 Views Asked by At

I am trying to use Cgo to export a function that initializes a zap logger. The function is written in Go but I am working towards the goal of being able to call this function in a C script and actually use the function in the C script. For example, my init function looks like this:

func initLogger() *zap.Logger {
    logger, _ := zap.NewProduction()
    return logger
}

Now, I want to be able to use this function in a c script by exporting it using cgo. So, I would do //export initLogger and then build a .so and .h file from this go file and link it to a c script to build an executable. So basically, in my C script, I want to be able to do something like :

logger = initLogger()
logger.Info("Hello World!")

This would log: {"level":"info","ts":1654031030.1591902,"caller":"go-kitLog/exLogger.go:14","msg":"Hello World!"}

The problem I am facing is that *zap.Logger does not export to C, it gives this error: Go type not supported in export: *zap.Logger. Have any of you possibly dealt with a similar situation? If so, how did you solve it?

0

There are 0 best solutions below