diff --git a/cmd/api/main.go b/cmd/api/main.go index da479f045..acfa03686 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -42,7 +42,7 @@ func run(port int) error { var client discoveryregistry.SvcDiscoveryRegistry client, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName, - config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10)) + config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) if err != nil { return err } diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index 82ee5842b..debe529f3 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -12,6 +12,7 @@ import ( relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" openKeeper "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry/zookeeper" @@ -48,7 +49,7 @@ func StartTransfer(prometheusPort int) error { } client, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openKeeper.WithFreq(time.Hour), openKeeper.WithRoundRobin(), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName, - config.Config.Zookeeper.Password), openKeeper.WithTimeout(10)) + config.Config.Zookeeper.Password), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) if err != nil { return err } diff --git a/pkg/common/log/zk_logger.go b/pkg/common/log/zk_logger.go index ccb116623..d69077d73 100644 --- a/pkg/common/log/zk_logger.go +++ b/pkg/common/log/zk_logger.go @@ -7,6 +7,10 @@ import ( type ZkLogger struct{} +func NewZkLogger() *ZkLogger { + return &ZkLogger{} +} + func (l *ZkLogger) Printf(format string, a ...interface{}) { ZInfo(context.Background(), "zookeeper output", "msg", fmt.Sprintf(format, a...)) } diff --git a/pkg/discoveryregistry/zookeeper/zk.go b/pkg/discoveryregistry/zookeeper/zk.go index 13ae21932..09fe6a6f0 100644 --- a/pkg/discoveryregistry/zookeeper/zk.go +++ b/pkg/discoveryregistry/zookeeper/zk.go @@ -39,6 +39,8 @@ type ZkClient struct { localConns map[string][]resolver.Address balancerName string RoundRobin + + logger *log.ZkLogger } type ZkOption func(*ZkClient) @@ -74,6 +76,12 @@ func WithTimeout(timeout int) ZkOption { } } +func WithLogger(logger *log.ZkLogger) ZkOption { + return func(client *ZkClient) { + client.logger = logger + } +} + func NewClient(zkServers []string, zkRoot string, options ...ZkOption) (*ZkClient, error) { client := &ZkClient{ zkServers: zkServers, @@ -87,7 +95,7 @@ func NewClient(zkServers []string, zkRoot string, options ...ZkOption) (*ZkClien for _, option := range options { option(client) } - conn, eventChan, err := zk.Connect(zkServers, time.Duration(client.timeout)*time.Second, zk.WithLogInfo(true), zk.WithLogger(&log.ZkLogger{})) + conn, eventChan, err := zk.Connect(zkServers, time.Duration(client.timeout)*time.Second, zk.WithLogInfo(true), zk.WithLogger(client.logger)) if err != nil { return nil, err } diff --git a/pkg/startrpc/start.go b/pkg/startrpc/start.go index 0d830c6f8..a821dbbab 100644 --- a/pkg/startrpc/start.go +++ b/pkg/startrpc/start.go @@ -7,6 +7,7 @@ import ( "time" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/network" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" @@ -27,7 +28,7 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c defer listener.Close() zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName, - config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10)) + config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) if err != nil { return utils.Wrap1(err) }