JSON logging in the go-sdk

Is there a canonical way to set up JSON logging in the go-sdk? I see that we can provide our own logger when setting up a worker and I’m wondering if there’s any precedent I can use to set up JSON logging before rolling my own.

I’m thinking about making a wrapper struct for the logrus logger which implements sdk-go/logger.go at master · temporalio/sdk-go · GitHub

Here’s some code I came up with in case anyone else stumbles on this question:

type Logger struct {
	logger *zap.SugaredLogger
}

func (l *Logger) Debug(msg string, keyvals ...interface{}) {
	l.logger.Debugw(msg, keyvals...)
}

func (l *Logger) Info(msg string, keyvals ...interface{}) {
	l.logger.Infow(msg, keyvals...)
}

func (l *Logger) Warn(msg string, keyvals ...interface{}) {
	l.logger.Warnw(msg, keyvals...)
}

func (l *Logger) Error(msg string, keyvals ...interface{}) {
	l.logger.Errorw(msg, keyvals...)
}

func GetTemporalClient(host string, port int) client.Client {
	cfg := zap.NewProductionConfig()
	cfg.Encoding = "json"
	logger, _ := cfg.Build()
	sugar := logger.Sugar()
	tClient, err := client.NewClient(client.Options{HostPort: fmt.Sprintf("%s:%d", host, port), Logger: &Logger{logger: sugar}})
	if err != nil {
		log.Fatalf("could not create temporal client: %s", err)
	}

	return tClient
}

I did some testing with logrus and consistently hit problems with zap so I ended up making a wrapper around a zap logger which will hopefully not have the same problems.

I added example to our samples repo. Thanks for the code snippet, I partially used it.