user18160597
user18160597

Reputation: 11

How to disable or redirect logs from console printed by 3rd party libraries which are using Go native zk package?

I'm building a CLI using the Cobra library in Go. According to my requirements, I need to use the zk package which is the Native ZooKeeper client for Go. The problem is that the logs from this 3rd party is getting printed on the console. I only want the logs of my own cobra application (using the fmt package) to get printed. I don't want the logs from the zk package to be displayed.

Was wondering what can be some approaches used for preventing the display of these logs.

Upvotes: 1

Views: 34

Answers (1)

Jory Geerts
Jory Geerts

Reputation: 1977

The zk package allows you to pass a customer Logger as a connection option, like this:

type yourOwnLogger struct {}
func (yourOwnLogger) Printf(string, ...interface{}) {
    // Do what you want with the log message here
}

func main() {
    zkc := zk.Connect([]string{"127.0.0.1"}, time.Second, zk.WithLogger(yourOwnLogger{}))
}

The logger needs to implement zk.Logger, which is an interface that requires one func: Printf(string, ...interface{}).

Their default simply forwards the log to log.Printf(), but you can do whatever you want (ignore it, write it to something else, parse it, whatever you need).

Upvotes: 0

Related Questions